$(document).ready(function(){
	var dt = new Date().getTime();
/* for static.html */
	/* Content loading */
	$('#iframe').html('');
	$('#iframe').load(root_dir+'suggestBlock.html?v'+dt, function() {
		$("#sugjestStringInput").val(sSuggestText);
		init();
		var str = sUrl + 'searchFromSuggest/' ;
		$('#filter').attr( "action", sIframeUrl);
		$('#filter').append('<input type="hidden" name="uri" value="' + str + '">');
	});

	$('#getListPage').live('click', function(){
		$('#iframe').html('');
		$('#iframe').load(root_dir+'listBlock.html?v'+dt, function() {
			buildList();
			init();
			var str = sUrl + 'search/' ;
			$('#filter').attr( "action", sIframeUrl);
			$('#filter').append('<input type="hidden" name="uri" value="' + str + '">');
		});
		return false;
	});
	
	$('#getSuggestPage').live('click', function(){
		$('#iframe').load(root_dir+'suggestBlock.html?v'+dt, function() {
			init();
			var str = sUrl + 'searchFromSuggest/' ;
			$('#filter').attr( "action", sIframeUrl);
			$('#filter').append('<input type="hidden" name="uri" value="' + str + '">');
		});
		return false;
	});
	/* Content loading */
});      

function buildList() {
	loadTravelTypes();
	loadTravelLocations();
}

function loadTravelTypes() {
	var dt = new Date().getTime();
	if(typeof(jsTravelType) == 'object'){
		var content = $('<div>');
		$("<div>").load(root_dir+"blocks.html?v="+dt+"#travelTypeBLock", function() {
			var obj = $(this).find("#travelTypeBLock");
			var i = 0;
			$.each(jsTravelType, function(index, value) {
				  if(i != 0) {
					  $("#RootBlock").append('<div class="stRootSpacer"></div>');
				  }
				  i = i + 1;
			      var x = obj.clone();
			      x.attr("id", "stShow"+index);
			      x.addClass("stImg"+index);
			      x.addClass("stRootBlock");
			      x.find("span.temp").replaceWith(value);
			      $("#RootBlock").append(x);
			});

		});
	}
}

function builChecksObj(obj, name) {
	var objx = obj.clone();
	objx.removeAttr('id');
	objx.find('#cehckUncheckCheck').attr('id', 'stParent'+name);
	objx.find('#cehckUncheckUncheck').attr('id', 'stParent'+name);
	return objx;
}

function buildBeach(obj, i, v) {
	var result = $("<div>").clone();
	var dl = obj.find('#dlListBlock');
	var dt = obj.find('#dtListBlock');
	var dd = obj.find('#ddListBlock');
	var j = 0;
	$.each(v['data'], function(index, value) {
		j = j+1; 
		var dlx = dl.clone();
		var dtx = dt.clone();
		dtx.find('#dtInput').attr('name', 'search[country][]').attr('value', value['code']).addClass("stParentCheckbox stParent"+i).attr('id','stCheckbox'+i+index);
		dtx.find("span.temp_country").replaceWith(value['name']);
		dlx.find('#dtListBlock').remove();
		dlx.find('#ddListBlock').remove();
		dlx.removeAttr('id').addClass("sgListBlock" + i);
		dlx.append(dtx);
		var country = index; 
		$.each(value['data'], function(index, value) {
			ddx = dd.clone();
			ddx.attr('id','Item'+i+country+'List');
			ddx.find('#ddInput').attr('name', 'search[city][]').attr('value', value['code']).addClass("stCheckbox"+i+country+"List").removeAttr("id");
			ddx.find("span.temp_regionas").replaceWith(value['name']);
			dlx.append(ddx);
		});
		if(j > maxCols) {
			j = 1;
			result.append('<div class="border_clear" style="height:15px;"></div>');
		}
		result.append(dlx);
	});
	result.append('<div class="border_clear" style="height:15px;"></div>');
	return result;
}

function buildskiing(obj, i, v) {
	var result = $("<div>").clone();
	var dl = obj.find('#dlListBlock');
	var dt = obj.find('#dtListBlock');
	var dd = obj.find('#ddListBlock');
	var j = 0;
	$.each(v['data'], function(index, value) {
		j = j+1; 
		var dlx = dl.clone();
		var dtx = dt.clone();
		dtx.find('#dtInput').attr('name', 'search[country][]').attr('value', value['code']).addClass("stParentCheckbox stParent"+i).attr('id','stCheckbox'+i+index);
		dtx.find("span.temp_country").replaceWith(value['name']);
		dlx.find('#dtListBlock').remove();
		dlx.find('#ddListBlock').remove();
		dlx.removeAttr('id').addClass("sgListBlock" + i);
		dlx.append(dtx);
		var country = index; 
		$.each(value['data'], function(index, value) {
			ddx = dd.clone();
			ddx.attr('id','Item'+i+country+'List');
			ddx.find('#ddInput').attr('name', 'search[transport][]').attr('value', value['code']).addClass("stCheckbox"+i+country+"List").removeAttr("id");
			ddx.find("span.temp_regionas").replaceWith(value['name']);
			dlx.append(ddx);
		});
		if(j > maxCols) {
			j = 1;
			result.append('<div class="border_clear" style="height:15px;"></div>');
		}
		result.append(dlx);
	});
	result.append('<div class="border_clear" style="height:15px;"></div>');
	return result;
}

function buildroundtrip(obj, i, v) {
	var result = $("<div>").clone();
	var dl = obj.find('#dlListBlock');
	var dt = obj.find('#dtListBlock');
	var dd = obj.find('#ddListBlock');
	var j = 0;
	$.each(v['data'], function(index, value) {
		j = j+1; 
		var dlx = dl.clone();
		var dtx = dt.clone();
		dtx.find('#dtInput').attr('name', 'search[dcountry][]').attr('value', value['code']).addClass("stParentCheckbox stParent"+i).attr('id','stCheckbox'+i+index);
		if(value['rating'] > 0) {
			dtx.find("span.temp_country").replaceWith("<b>"+value['name']+"</b>");
		} else {
			dtx.find("span.temp_country").replaceWith(value['name']);
		}
		dlx.find('#dtListBlock').remove();
		dlx.find('#ddListBlock').remove();
		dlx.removeAttr('id').addClass("sgListBlock" + i);
		dlx.append(dtx);
		var country = index; 
		/*
		$.each(value['data'], function(index, value) {
			ddx = dd.clone();
			ddx.attr('id','Item'+i+country+'List');
			ddx.find('#ddInput').attr('name', 'search[transport][]').attr('value', value['code']).addClass("stCheckbox"+i+country+"List").removeAttr("id");
			ddx.find("span.temp_regionas").replaceWith(value['name']);
			dlx.append(ddx);
		});
		*/
		if(j > maxCols) {
			j = 1;
			result.append('<div class="border_clear" style="height:15px;"></div>');
		}
		result.append(dlx);
	});
	result.append('<div class="border_clear" style="height:15px;"></div>');
	return result;
}

function buildlonghaul(obj, i, v) {
	var result = $("<div>").clone();
	var dl = obj.find('#dlListBlock');
	var dt = obj.find('#dtListBlock');
	var dd = obj.find('#ddListBlock');
	var j = 0;
	$.each(v['data'], function(index, value) {
		j = j+1; 
		var dlx = dl.clone();
		var dtx = dt.clone();
		dtx.find('#dtInput').attr('name', 'search[dcountry][]').attr('value', value['code']).addClass("stParentCheckbox stParent"+i).attr('id','stCheckbox'+i+index);
		dtx.find("span.temp_country").replaceWith(value['name']);
		dlx.find('#dtListBlock').remove();
		dlx.find('#ddListBlock').remove();
		dlx.removeAttr('id').addClass("sgListBlock" + i);
		dlx.append(dtx);
		var country = index; 
		/*
		$.each(value['data'], function(index, value) {
			ddx = dd.clone();
			ddx.attr('id','Item'+i+country+'List');
			ddx.find('#ddInput').attr('name', 'search[transport][]').attr('value', value['code']).addClass("stCheckbox"+i+country+"List").removeAttr("id");
			ddx.find("span.temp_regionas").replaceWith(value['name']);
			dlx.append(ddx);
		});
		*/
		if(j > maxCols) {
			j = 1;
			result.append('<div class="border_clear" style="height:15px;"></div>');
		}
		result.append(dlx);
	});
	result.append('<div class="border_clear" style="height:15px;"></div>');
	return result;
}



function loadTravelLocations() {
	var dt = new Date().getTime();
	if(typeof(jsTravelLocations) == 'object'){
		$("<div>").load(root_dir+"blocks.html?v="+dt+" #listBlock", function() {
			var listBlock = $(this).find("#listBlock");
			$("<div>").load(root_dir+"blocks.html?v="+dt+" #listBlockInner", function() {
				var listBlockInner = $(this).find("#listBlockInner");
				$("<div>").load(root_dir+"blocks.html?v="+dt+" #cehckUncheck", function() {
					var cehckUncheck = $(this).find("#cehckUncheck");
					$.each(jsTravelLocations, function(index, value) {
						var ia = index;
						var va = value;
						var listBlockX = listBlock.clone();
						var cehckUncheckX = builChecksObj(cehckUncheck, ia);
						switch(ia) {
							case 'beach':
								var listBlockInnerx = buildBeach(listBlockInner, ia, va);
							  break;
							case 'roundtrip':
								var listBlockInnerx = buildroundtrip(listBlockInner, ia, va);
								break;
							case 'skiing':
								var listBlockInnerx = buildskiing(listBlockInner, ia, va);
								break;
							case 'longhaul':
								var listBlockInnerx = buildlonghaul(listBlockInner, ia, va);
								break;
							default:
								var listBlockInnerx = buildBeach(listBlockInner, ia, va);
								break;
						}
						listBlockX.addClass("stShowList stShow"+ia+"List cleara");
						listBlockX.find('#tmp_checkUncheck').replaceWith(cehckUncheckX);
						listBlockX.find('#tmp_listBlockInner').replaceWith(listBlockInnerx);
						$('#insertTarget').before(listBlockX);
						init();
					});
				});
			});
		});
	}
}


function init() {
    
	/* suggest search */
	$("#sugjestStringInput").autocomplete(root_dir+'proxy.php?lang=' + sLang, {
        width: 510,
        multiple: false,
		dataType: "json",
		parse: function(data) {
		    return $.map(data, function(row) {
		        return {
		            data: row,
		            value: row.value
		        }
		    });
		},
		formatItem: function(item) {
		    return format(item);
		}
	}).result(function(e, item) {
	$("#sugjestStringInput").val(item.value);
	$("#searchSuggjest").html('');
	$("#searchSuggjest").append(
	    '<input type="hidden" name="search[codetype]" value="' + item.codetype + '">' +
	    '<input type="hidden" name="search[code]" value="' + item.code + '">' +
	    '<input type="hidden" name="search[value]" value="' + item.value + '">' +
	    '<input type="hidden" name="search[value_web]" value="' + item.value_web + '">' +
	    '<input type="hidden" name="search[city_code]" value="' + item.city_code + '">' +
	    '<input type="hidden" name="search[country_code]" value="' + item.country_code + '">');
	});
	
	function format(item) {
		return '<span><b>' + item.value + '</b></span>';
	}
	
	$("#sugjestStringInput").focus(function() {
		if ($(this).val() == sSuggestText) { 
			$(this).val(''); 
			$(this).addClass('black');
		}
	});
	$("#sugjestStringInput").blur(function() {
		if ($(this).val() == '') { 
			$(this).val(sSuggestText); 
			$(this).removeClass('black');
		}
	});
	/* suggest search */
 
	/*date picker*/
	if(sLang != 'en') {
		$.datepicker.setDefaults($.datepicker.regional[sLang]);
	}
	
	/*date picker */

    //SHOW/hide searchparams
    $('.showMore').live('click', function() {
   	 $(".showMore").hide();
   	 $(".showLess").show();
   	 $('.detailSearchParams').slideDown('normal', function(){
   		 setHeight();
   	 });
   	 return false;
    });

    $('.showLess').live('click', function() {
   	 $(".showMore").show();
   	 $(".showLess").hide();
   	 $('.detailSearchParams').slideUp('normal', function(){
   		 setHeight();
   	 });
   	 return false;
    });
    $('.showLess').hide();
    
    $('.paramsDefault select, .paramsDefault input, .childSelect').change(function() {
   	 $('.detailSearchParams').slideDown('normal', function(){
   		 $(".showMore").hide();
       	 $(".showLess").show();
   		 setHeight();
   	 });
    });
    //SHOW/hide searchparams	
	

	//select trip
	$(".stRootBlock").live("click", function() {
		showDiv = $(this).attr('id');
		$(".stShowList").hide()
		$(".stRootBlock").removeClass('stActive');
		$(this).addClass('stActive');
		$("."+showDiv+"List").show();
			setHeight();
	});
	
	$(".checkAll").live('click', function () {
		child = $(this).attr('id');
		$('.'+child).attr('checked', 'checked');
		$('.'+child).each(function (){ 
			child = $(this).attr('id');
			if($(this).is(":checked")) {
				$('.'+child+'List').attr('checked', 'checked');
			} else {
				$('.'+child+'List').removeAttr('checked');
			}	
		});
	});
	
	$(".uncheckAll").live('click', function () {
		child = $(this).attr('id');
		$('.'+child).removeAttr('checked');
		$('.'+child).each(function (){ 
			child = $(this).attr('id');
			if($(this).is(":checked")) {
				$('.'+child+'List').attr('checked', 'checked');
			} else {
				$('.'+child+'List').removeAttr('checked');
			}	
		});
	});
	
	$(".stParentCheckbox").live("click", function () {
		child = $(this).attr('id');
		if($(this).is(":checked")) {
			$('.'+child+'List').attr('checked', 'checked');
		} else {
			$('.'+child+'List').removeAttr('checked');
		}
	});
	
	$('#stShowbeach').addClass('stActive');
	$('.stShowbeachList').slideDown("fast", function(){
		setHeight();
	});
	//select trip
    
	$('.date-pick').datepicker({dateFormat: 'yy-mm-dd','minDate': 0});
      
     $("#search_childs").live("click", function(){
        iSelected = $(this).val();
        if (iSelected>0)
        {
        	$(".childSelectBlockMain").show();
            $("#childSelectBlock").show();
            for( i=0; i < 3; i++)
            {
                if (i<iSelected)
                    $("#search_childs_age_"+i).show();
                else
                    $("#search_childs_age_"+i).hide();
            }
        }
        else
        {
        	$(".childSelectBlockMain").hide();
            $("#childSelectBlock").hide();
            for( i=0; i < 3; i++)
            {
                $("#search_childs_age_"+i).hide();
            }
        }
   	 setHeight();
     });


    $('#sugjestStringInput').keypress(function(e) {
        if(e.which == 13) {
            $(this).blur();
            $('#filter').submit();
        }
    });
    
}

 function setHeight() {};

