/**
*	Some js
*/


var offset = 0;
var cont;




$(document).ready(function(){
	
	
	// lightbox
	
	$("a[@rel*=lightbox]").lightbox(); 
	
	
	// voblers - jaunās preces
	
	jaunas();
	
	$(window).resize(function() {
		jaunas();
	});
	
	
	
	
	if(location.hash.indexOf("showBasket") > 0) {
		showBasket(true);	
	}
	
	

	// veram vaļā hash (vajadzīgs Zīmoliem)
	
	var hash = location.hash.toUpperCase();	
	var el = $(hash);
	
	if(hash && el.length > 0) {
		el.toggle();		
	}
	
	
	
	// bind toggle for cat button images

	$(".button").hover(
		function(){
			
			if (this.className.indexOf("active") == -1) {
			
				$(".under", this).css("background-color", "red");
			}			
		},
		function(){

			if (this.className.indexOf("active") == -1) {
			
				$(".under", this).css("background-color", "#50504f");
				
			}
		}
	);




	// akselerators 

	$("#buttonRow .button").click(function(el) {	
		var link = $("a", this).attr("href");
		window.location.href = link;
	})


	$("#side li").click(function(el) {	
		var link = $("a", this).attr("href");
		window.location.href = link;
	})


	/*
		if($('a.zoom').length > 0)
			$('a.zoom').zoomimage();
	*/


	hs.graphicsDir = 'js/highslide/highslide/graphics/';
        hs.outlineType = 'rounded-white';	

	
	
	 $('a.zoom').each(function() {
	    this.onclick = function() {
	      return hs.expand(this);
	    };
	 });	
	


	// parametru tabula (zebra)
	
	$("table.parms tbody tr:odd").addClass("odd");



	// bilžu parslēdzējs
	
	$(".thumb").click(function(el) {
		
		var curr =  $("img", this).attr("src").replace("&width=100", "&width=270");;		
		var cont = $("#image img").attr("src", curr);
		
		var href =  $("img", this).attr("src").replace("&width=100", "&width=800");;
		$("#image a").attr("href", href);
		
		//console.log($('a.zoom'))
		//$('a.zoom').zoomimage();
		
	});
	
	
	$(".thumb").hover(
		function(){
			
			$(this).css("border", "1px solid red");
						
		},
		function(){
			
			$(this).css("border", "1px solid #e1eff8");
			
		}
	);
	
	
	
	// kategorijas / grozs

	$(".buttons div").click(function(nr, tests) {
			
		var idx = $(".buttons div").index(this);
		
		$(".buttons div").removeClass("active");
		$(this).addClass("active");

		$("#side>div").hide();
		$("#side>div:eq("+idx+")").show();
				
	});

	
	
	
	$("input#search").keydown(function(e) {		
		if(e.keyCode == 13)
			doSubmit("search");
	});
	
	$("input#user").keydown(function(e) {		
		if(e.keyCode == 13)
			doSubmit("login");
	});	

	$("input#pass").keydown(function(e) {		
		if(e.keyCode == 13)
			doSubmit("login");
	});		
	
});






function jaunas() {
	
	var width = $(window).width();
	
	if(width < 1150) {
		$("div#jaunas").hide();
	} else {
		$("div#jaunas").show();
	}
}






function numbersonly(e) {
	
	var e=window.event || e
	var code=e.charCode || e.keyCode
	
	var i = String.fromCharCode(code);
	
	if(i.match(/\d|\./g) || code == 8) {
		return true;
	} else {
		return false;
	}
}








function dialog(q) {
	
	$("input.tema").val(document.title);
	
	switch(q) {
	
		case "question":
			$.modal($('#question'));
			break;
			
		case "callBack":
			$.modal($('#callBack'));
			break;
			
		case "send":
			$.modal($('#send'));
			break;
	}
	
	//http://www.ericmmartin.com/projects/simplemodal/

	
}





function izsole(title, name, email, phone) {

	var bid = parseFloat($("input#bid").val());
	
	
	if(bid > 0) {
	
		// set bid and title
		
		$("#likmeUz").html( title );
		$("#likmeVertiba").val( bid );
		
		$("#izsole .name").val(name);
		$("#izsole .email").val(email);
		$("#izsole .phone").val(phone);
		
		$.modal($('#izsole'));
	}
}




function dialogCheck(el) {
	
	var err = false;
	
	$("input,textarea", el).each(function(n, item) {
			
		if( $(item).val().length == 0)
			err = true;
	})
	
	if(err) {
		alert("Kāds lauciņš palicis tukšs ;)");
		return false;
	}
}









function showBasket(show) 
{
	
	var buttons = $(".buttons div");
	
	// reset
	
	buttons.removeClass("active");
	$("#side>div").hide();
	
	if(show) {
		
		// do show

		var idx = 1;
		$( buttons.get(idx) ).addClass("active");		
		
		
	} else {
		
		// show categories..

		var idx = 0;
		$( buttons.get(idx) ).addClass("active");			
		
	}
	
	
	$("#side>div:eq("+idx+")").show();
	
}












function addToBasket(item)
{
	basket("change", item, 1);
}



function changeBasket(item, inp)
{
	basket("change",item, inp.value);
}


function removeFromBasket(item)
{
	basket("remove",item);
}



function basket(cmd, item, qtn)
{
	
	$("#basket").fadeTo(1, 0.33);
	
	$.ajax({
		type: "POST",
		url: "feed.php",
		data: "cmd=" + cmd + "&item=" + item + "&skaits=" + qtn,
		dataType: "json",
		success: function(json){
						
			if(json != null) {

				showBasket(true);
				$("#basket").fadeTo("fast", 1);
				
				$.each(json, function(name, value) {
					
					if ($(name).length > 0) {
						$(name).html(value);
					}
					
				});
				
			}

			
		},
		error: function (msg) {
			alert(msg.responseText);
		}
	});	
	
	
}








var skip = 0;

function doSubmit(el)
{

	if (el == "basket") {
		document.forms.main.action = "?view=basket";
		document.forms.main.submit();
		return true;
	}	

	
	if (el == "forgot") {
		var id = ($("body").attr("id")).match(/\d+/)[0];
		document.forms.main.action = "?id=" + id + "&user=forgot";
		document.forms.main.submit();
		return true;
	}		

	
	if (el == "save") {
		document.forms.main.action = "?id=1080&user=profile";
		document.forms.main.submit();
		return true;
	}		
	
	
	if (el == "login") {
		var id = ($("body").attr("id")).match(/\d+/)[0];
		document.forms.main.action = "?id=" + id + "&user=profile";
		document.forms.main.submit();
		skip=1
		return true;
	}	
	

	
	var str;
	
	if($(el))
		str = $("#" + el).val();
		
	if(!str || str.lenght < 1)
		return false;

	
	if (el == "search") {
		document.forms.main.action = "?search=" + str;
		document.forms.main.submit();
		skip = 1;
		return false;
	}	
	
	
	if (el == "zimols" && skip == 0) {
		document.forms.main.action = "?zimols=" + str;
		document.forms.main.submit();
		return false;
	}

	
	if (el == "orderBy") {
		//document.forms.main.action = "index.php?orderBy=" + str;
		document.forms.main.submit();
	}	

	
	return false;
}









function openZimoli(el) {
		
	$(el).toggle();
	
}





