var courseCounter = 0;

$(document).ready(
    function()
    {		
	
    	courseCounter = $('#courseCounter').html();
    	
		$('img.compareAdd').click(
		    function()
		    {
		         $.post("/DEV/course_compare.php", { add_course: "true", compareCourse_ID: $(this).parent().children("h3").children("a").attr("href").replace("test_course.php?course_id=","") } );
		         $("body").append('<div class="comparingCourse"><a href="/DEV/course_compare.php" target="_Blank">COMPARE</a></div>');
		     }
		);
		
		$("img.courseCompareDelete").click(
		    function()
		    {
		        var id = $(this).attr("alt").replace("delete ","");
		        $.post("/DEV/course_compare.php", { delete_course: "true", compareCourse_ID: id } );
		        $(".course_"+id).remove();
		    }
		);
		
		
		
		$('.toggleSearchFormButton').parent().children("div").children().hide();
		$('.toggleSearchFormButton').click(
		    function()
		    {
		        if($(this).parent().children("div").children().css('display') == "none")
		        {
		            //$(this).children("img").attr("src","/images/icons/minus.gif");
		            $(this).parent().children("div").children().show(
	            													'1000'
	            													);
		        }
		        else
		        {
		            //$(this).children("img").attr("src","/images/icons/plus.gif");
		            $(this).parent().children("div").children().hide(
		            												'1000'
		            												);
		        }
		        //alert("das");
		        //alert(courseCounter);
		        $('#courseCounter').html(courseCounter);
		        return false;
		    }
		   
		)

		$('#typeRadio').click(function(){$('#selectedCountry').attr('disabled','disabled');$('#selectedType').attr('disabled','');});
		$('#countryRadio').click(function(){$('#selectedType').attr('disabled','disabled');$('#selectedCountry').attr('disabled','');});
		/* SearchPage Crawler */
		
		var countryList = new Array();
		var cityList 	= new Array();
		var typeList 	= new Array();
		var langList 	= new Array();
		
		$.protify(countryList,true);
		$.protify(cityList,true);
		$.protify(typeList,true);
		$.protify(langList,true);
		
		var tCountry = "";
		var tCity	 = "";
		var tLang	 = "";
		var tType	 = "";

		// ":not([safari])" is desirable but not necessary selector
		//$('input:checkbox').checkbox({cls:'jquery-cactus-checkbox'});   

	var selectedCountry = $('#selectedCountry').val();
	var selectedType = $('#selectedType').val();
//alert(selectedCountry);
    
	getDests($('#selectedLanguage').val(),selectedCountry);
	getTypes($('#selectedLanguage').val(),selectedType);
    
	$('#selectedLanguage').change(
    		function()
    		{
//alert(selectedCountry);
	var selectedCountry = $('#selectedCountry').val();
	var selectedType = $('#selectedType').val();
    			getDests($(this).val(),selectedCountry);
			getTypes($(this).val(),selectedType);
    		}
    	);
    
});

function getDests(lang,selectedCountry)
{		   
	$.getJSON("/DEV/ajax_getter.php?domains[]=[CL]&domains[]=[FLUK]&domains[]=[FLUSA]",
	{language: lang, RETURN: 'countries',selectedCountry:selectedCountry},
    		function(j)
    		{
			var options = '';
			var disabled = '';
	        	var selected = '';

//			options += '<option value="">All Countries</option>';
//alert(j.selectedIndex);
	        	for (var i = 0 ; i < j.length ; i++)
	        	{
	        		disabled = '';
	           		selected = '';
	           		
			//	if(j[i].optionDisabled == "true")
			//	{
			//	disabled = ' disabled="disabled" ';
			//	}
//alert(i + ' ' + j[i].optionDisplay + ' ' + j[i].optionSelected);
//alert(j[i].optionSelected);
//if(selectedCountry == j[i].optionDisplay)
//{

//}
				if (j[i].optionSelected)
				{
//alert(i + ' ' + j[i].optionDisplay + ' not true');
					selected = ' selected="selected" ';
				}
				else if(j[i].optionSelected == "true")
				{
alert(i + ' ' + j[i].optionDisplay + ' "true" ' + selectedCountry);
					selected = ' selected="selected" ';
				}
				else if(j[i].optionSelected == 'true')
				{
alert(i + ' ' + j[i].optionDisplay + ' true ' + selectedCountry);
					selected = ' selected="selected" ';
				}
				else
				{
//alert(i + ' ' + j[i].optionDisplay + ' x ' + selectedCountry);
				}
				options += '<option '+selected+''+disabled+' class="'+ j[i].optionStyleClass + '"  value="' +j[i].optionValue+ '">' + j[i].optionDisplay + '</option>';
	    	      }
	             //alert(options);
	    	     $("#selectedCountry").html(options);
    	    });
}

function getTypes(lang,selectedType)
{
	$.getJSON("/DEV/ajax_getter.php?domains[]=[CL]&domains[]=[FLUK]&domains[]=[FLUSA]",
	{language: lang, RETURN: 'types',selectedType:selectedType},
		function(j)
		{
	        	var options  = '';
	        	var selected = '';
	             
//			options += '<option value="">All Types</option>';
         
			for (var i = 0; i < j.length ; i++)
	        	{               
	        		selected = '';
	           		if(j[i].optionSelected == "true")
				{
					selected = ' selected="selected" ';
				}
				options += '<option '+selected+' value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
		     $("#selectedType").html(options);
			     
		   });
}


