/** Meklēšanas papildus parametri **/

$("document").ready(function(){

	var catLinks = $("#search_options div.first").find("a");
	var optionsSelected = getCookie("optionsSelected");
	var divContainer = $("#search_options div.second");
	var checkBoxCont = divContainer.find("div");
	var selectedItems = $("#search_options div.third");
	
	$(catLinks).each(function(i, val){
		$(this).click(function(event){
			var id = $(this).attr("id");
			var catId = id.split("_",2)[1];
			
			$("#servCatId_"+catId).show();
			hideOtherServiceGroups(catId);
			$(this).addClass("active");
			removeActiveClass(catId);
			
			event.preventDefault();
		});
	});

	function hideOtherServiceGroups(current){
		var allServiceGroups = $("#search_options div.second").find("div");

		$(allServiceGroups).each(function(i, val){
			var fieldId = $(this).attr("id");
			var id = fieldId.split("_",2)[1];
				if (id !== current){
					$("#servCatId_"+id).hide();
				}
		});
	}

	function removeActiveClass(current){
		var allLinks = $("#search_options div.first").find("a");
		$(allLinks).each(function(){
			var linkId = $(this).attr("id");
			var id = linkId.split("_",2)[1];
				if(id !== current){
					$("#catid_"+id).removeClass("active");
				}
		});
	}
	
	/** Cookijos saglabātās opcijas vajadzētu ieķeksēt  **/
	var optionsFromCookie = [];
	if(optionsSelected){
		optionsFromCookie = optionsSelected.split("|");
		checkBoxCont.find(":checkbox").each(function(){
			for(var j=0; j<optionsFromCookie.length-1; j++){
				if($(this).attr("id").split("_")[1] == optionsFromCookie[j]){
					$(this).attr("checked", true);
				}
			}
		});
	}
	/*****  Jāsaglabā izvēlētie čekboksi COOKIES  ******/
	function addOptionsToCookie(){
		var checkBoxes = checkBoxCont.find(":checkbox");
		var selectedCheckBoxes = "";
		$(checkBoxes).each(function(){
			if($(this).attr("checked")){
				selectedCheckBoxes += $(this).attr("id").split("_")[1] + "|";
			}
		});
		saveSelectedOptions(selectedCheckBoxes);
	}
	/***********************************************/
	function saveSelectedOptions(value){
		var expiryDate=new Date();
		expiryDate.setTime(expiryDate.getTime() + (1000*60*60*24*1));
		setCookie('optionsSelected', value, expiryDate, '', '');
	}
	
	
	/******************************************************/
	/** Čekboksu uzvedība 				         *************/
	/******************************************************/
	$(checkBoxCont).each(function(){
		$(this).hide();
		var checkLabels = $(this).find("label");
		
 		$(checkLabels).each(function(i, val){
			/** pievienojam checkbox click eventu **/
			var checkBoxItem = $(this).find(":checkbox");
			
			checkBoxItem.click(function(){
				var id = $(this).attr("id").split("_", 2)[1];
				var parentText = $(this).parent().text();
				
				if($(this).attr("checked")){
					selectedItems.append('<div class="optSelected" id="selectedOpt_'+id+'">'+((parentText.length > 18) ? parentText.substr(0,18)+'..' : parentText)+'</div>');
					// delete bilde
					selectedItems.find("#selectedOpt_"+id).append('<img src="http://www.visiservisi.lv/img/action_delete.png">');
					selectedItems.find("#selectedOpt_"+id).find("img").click(function(){
						$("#pakalp_"+id).removeAttr("checked");
						addOptionsToCookie();
						$(this).parent().remove();
					});
					addOptionsToCookie();
				}else{
					selectedItems.find("#selectedOpt_"+id).remove();
				}
			});
			/**
			Lapai ielādējoties, ieķeksētie čekboksi jāattēlo labajā konteinerā...
			**/
			var id = checkBoxItem.attr("id").split("_", 2)[1];
			var checkText = checkBoxItem.parent().text();
			if(checkBoxItem.attr("checked")){
				selectedItems.append('<div class="optSelected" id="selectedOpt_'+id+'">'+((checkText.length > 18) ? checkText.substr(0,18)+'..' : checkText)+'</div>');
					// delete bilde
					selectedItems.find("#selectedOpt_"+id).append('<img src="http://www.visiservisi.lv/img/action_delete.png">');
					selectedItems.find("#selectedOpt_"+id).find("img").click(function(){
						$("#pakalp_"+id).removeAttr("checked");
						addOptionsToCookie();	
						$(this).parent().remove();
					});
			}
		});
		
	});
});
