jQuery.noConflict();

jQuery(document).ready(function($){
	document.usForm.reset();							
	if($("#om_us").is(":checked")){
		checkAllSearchItems();
	}else{
		if(document.usForm.is_us.value==1){
			checkAllSearchItems();
		}
	}
	$("#search_input").attr("disabled","");
	$("#search_input").focus(function(){	
		document.getElementById("search_input").style.backgroundImage = ""; 
	});
	$("#search_input").blur(function(){
		var inputImage = $("#inputImage").val();
		changeInputImage(inputImage);
	});
	/*if($("#currentPage").val()=="articles" &&)
	checkSubItems('newsNreviews');
	checkSubItems('mycommunity');*/
	$("#CloseCountry").click(function () {
		$("#changeCountryLayer").hide();
    });  
	$("#CloseOptions").click(function () {
		$("#moerOptionsLayer").hide();
    });  
	
	$("#om_us").click(function(){
		checkAllSearchItems();
	});
	
	/* for availability */
	$("input[name^='om_time_']").click(function(){
		$("#om_products").attr("checked", true);
		$("#om_availability").attr("checked", true);		
		
		var l = $("input[name^='om_time_']:checkbox:checked").length;
		
		if(l == 0) {			
			$("#om_products").attr("checked", false);		
			$("#om_availability").attr("checked", false);			
		}		
		checkMainItems('us');
	});
	
	$("#om_availability").click(function(){
		if($(this).is(":checked")) {
			$("#om_products").attr("checked", true);		
			$("input[name^='om_time_']").attr("checked", true);
		} else {
			$("#om_products").attr("checked", false);					
			$("input[name^='om_time_']").attr("checked", false);
		}
		checkMainItems('us');
	});
	
	$("#om_products").click(function(){
		if($(this).is(":checked")) {		
			$("#om_availability").attr("checked", true);
			$("input[name^='om_time_']").attr("checked", true);			
		} else {
			$("#om_availability").attr("checked", false);
			$("input[name^='om_time_']").attr("checked", false);			
		}
	});
	/* end availability */


	if($("#currentPage").val() == "products"){
		changeInputImage("products");
	}

});

//check or uncheck "search entire site"
function checkAllSearchItems(){ 
	jQuery(document).ready(function($){
		if($("#om_us").is(":checked")){
			//$("#inputImage").val("index");
			//changeInputImage("index");
			$(".checkbox_class:checkbox").attr("checked","checked");	
		}else{
			//changeInputImage($("#currentPage").val());
			$(".checkbox_class:checkbox").attr("checked","");
		}
		getInputImageName();
	});  
}

// checked or unchecked category all subcategory
function checkSubItems(parentName){
	jQuery(document).ready(function($){
	if($("#om_"+parentName).is(":checked"))
		$("."+parentName+" > :checkbox").attr("checked","checked");
	else
		$("."+parentName+" > :checkbox").attr("checked","");

	});  
	
	//if subitems is checked/unchecked, check universal search checkbox
	checkMainItems('us');
	
}
// check if the "main" category need to be checked
function checkMainItems(parentName){
	jQuery(document).ready(function($){
									
		var totalCheckbox =$("."+parentName+" > :checkbox").length;
		var totalChecked=0;
		$("."+parentName+" > :checkbox").each(function(i){
			if(this.checked){
				totalChecked+=1;	
			}
		});
		if(totalChecked==totalCheckbox){
			$("#om_"+parentName).attr("checked","checked");
		}else{
			$("#om_"+parentName).attr("checked","");
		}
		//if main item is checked/unchecked, check universal search checkbox
		if(parentName !="us"){
			checkMainItems('us');
		}else{
			getInputImageName();
		}
		
	});  
}



function getInputImageName(){
	jQuery(document).ready(function($){
		//count how any is checked
		var totalMainChecked=0;
		$(".us > :checkbox").each(function(i){
			if(this.checked){
				totalMainChecked+=1;	
			}
		});
		var totalNewsNreviewsChecked=0;
		$(".newsNreviews > :checkbox").each(function(i){
			if(this.checked){
				totalNewsNreviewsChecked+=1;	
			}
		});
		var totalMycommunityChecked=0;
		$(".mycommunity > :checkbox").each(function(i){
			if(this.checked){
				totalMycommunityChecked+=1;	
			}
		});
		
		//alert(totalMainCheckbox+ "  "+ totalNewsNreviewsCheckbox+ "  "+ totalMycommunityCheckbox);
		//alert(totalMainChecked+ "  "+ totalNewsNreviewsChecked+ "  "+ totalMycommunityChecked);
		if($("#om_us").is(":checked")){
				$("#inputImage").val("index");
				changeInputImage("index");
				
		}else if(totalMainChecked>0 ||(totalNewsNreviewsChecked>0 && totalMycommunityChecked>0 ) ){
			if(totalMainChecked==1 ){
				if($("#om_products").is(":checked") && totalNewsNreviewsChecked==0 && totalMycommunityChecked==0){
					$("#inputImage").val("products");
					changeInputImage("products");
				}else if($("#om_newsNreviews").is(":checked")&& totalMycommunityChecked==0){
					$("#inputImage").val("articles");
					changeInputImage("articles");
				}else if($("#om_help").is(":checked")&& totalNewsNreviewsChecked==0 && totalMycommunityChecked==0){
					$("#inputImage").val("help");
					changeInputImage("help");
				}else if($("#om_mycommunity").is(":checked")&& totalNewsNreviewsChecked==0){
					$("#inputImage").val("community");
					changeInputImage("community");
				}else{
					$("#inputImage").val("custom");
					changeInputImage("custom");
				}
			}else{
			$("#inputImage").val("custom");
			changeInputImage("custom");
			}

		}else if(totalMainChecked==0 && totalNewsNreviewsChecked==0 && totalMycommunityChecked==0){
			$("#inputImage").val($("#currentPage").val());
			if($("#currentPage").val() == "index"){
				changeInputImage("products");
			}else{
				changeInputImage($("#currentPage").val());
			}
			
		}else if(totalNewsNreviewsChecked>1 || totalMycommunityChecked>1 ){
			$("#inputImage").val("custom");
			changeInputImage("custom");
		}else if(totalMainChecked==1 ){

				if($("#om_products").is(":checked")){
					$("#inputImage").val("products");
					changeInputImage("products");
				}else if($("#om_newsNreviews").is(":checked")){
					$("#inputImage").val("articles");
					changeInputImage("articles");
				}else if($("#om_help").is(":checked")){
					$("#inputImage").val("help");
					changeInputImage("help");
				}else{
					$("#inputImage").val("community");
					changeInputImage("community");
				}
		}else{
			if($("#om_reviws").is(":checked") ||$("#om_userspost").is(":checked")||$("#om_environmentalAwareness").is(":checked") ){
				$("#inputImage").val("articles");
				changeInputImage("articles");
			}else if($("#om_sitenews").is(":checked")){
				$("#inputImage").val("news");
				changeInputImage("news");
			}else if($("#om_helpsNtips").is(":checked")){
				$("#inputImage").val("tips");
				changeInputImage("tips");
			}else if($("#om_albums").is(":checked")){
				$("#inputImage").val("albums");
				changeInputImage("albums");
			}else if($("#om_images").is(":checked")){
				$("#inputImage").val("images");
				changeInputImage("images");
			}else if($("#om_users").is(":checked")){
				$("#inputImage").val("members");
				changeInputImage("members");
			}
			
		}
	
	}); 
}

function changeInputImage(imageName){
	jQuery(document).ready(function($){
	if($("#search_input").val()==""){
		var cssObj = {
			"background-image": "url(/en/images/silver/searchbox/searchbox_"+imageName+".gif)"
		}
		$("#search_input").css(cssObj);
	}
	});  
}
//display or hidden "more search options" and "country"
function toggleLayer(layer){
	jQuery(document).ready(function($){
	$("#"+layer).toggle();
	});  
}
function userLogout(){
	document.getElementById("operation").value="logout";	
}

function ChangeBarCountry(){
	document.countrybar.submit();
}

var $basket = jQuery.noConflict();
$basket(window).load(function() {
	$basket("#cart_bar_loading").hide();
	$basket("#cart_bar").show();
});
$basket(document).ready(function(){	
	$basket("a.get_html").colorbox({width:"530px", height:"750px", iframe:true, opacity:0.5});		
	$basket("a[href$='basket.php']").colorbox({width:"995px", height:"600px", iframe:true, opacity:0.5});	
	$basket("a[href='thickbox_category.php']").colorbox({width:"815px", height:"550px", iframe:true, opacity:0.5});	
	$basket("#inline").colorbox({width:"500px", height:"220px", inline:true, href:"#inline_example", open:true, overlayClose:false});
	$basket("a[href='aff_apply.html']").colorbox({width:"480px", height:"450px", iframe:true, opacity:0.5});			
});
	
var $auto = jQuery.noConflict();

$auto(document).ready(function() {
	function findValueCallback(event, data, formatted) {
		$auto("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}

	$auto("#search_input").autocomplete("autocomplete_search.php", {
		delay: 200,
		width: 350,
		minChars: 1,
		matchSubset: false,
		matchContains: false,
		selectFirst: false,
		cacheLength: 0,
		noCache: true,
		onItemSelect: findValue,
		onFindItem: findValue,		
		formatItem: function(row) {
			return "<div style='height:12px'><div style='float:left'>" + row[0] + "</div>";
		}
	});
	
	$auto("#search_string").autocomplete("autocomplete_search.php", {
		delay: 200,
		width: 350,
		minChars: 1,
		matchSubset: false,
		matchContains: false,
		selectFirst: false,
		cacheLength: 0,
		noCache: true,
		onItemSelect: findValue,
		onFindItem: findValue,		
		formatItem: function(row) {
			return "<div style='height:12px'><div style='float:left'>" + row[0] + "</div>";
		}
	});
			
	function findValue(li) {
    	if (li == null) return alert("No match!");
		$auto("#txtSymbol").val(li.extra[0]);
		$auto("#txtCName").val(li.extra[1]);
	}	

});
