function setContentMinHeight(){	
		var contentsHeight = $('#content').innerHeight();
		$('#header_container').css({"top":  contentsHeight});
}
	
function animatePromotions(postponeTime){
	//alert("animatePromotions, initialLabelDelay= "+initialLabelDelay)
	var initialLabelDelay = 900; // 1.5 sec
	var labelVisibilityTime = 6000; // 10 sec
	var defaultEffectDuration = 400; // 2/3 sec
	var slowEffectDuration = 600; // 1 sec	
	var totalPromotionVisibilityTime = slowEffectDuration + initialLabelDelay + (3 * defaultEffectDuration) + labelVisibilityTime - 900;
	//alert(totalPromotionVisibilityTime);

	var allPromotions = $('.promo_photo_holder');
	//var $tmpObj = allPromotions;
		allPromotions.each(function(i){
			//var the_holder = $(this);
			var promo_photo = $(this).find('.promo_photo');
			var promo_label = $(this).find('.promo_label');			
			//the_holder.css('display','block');
			promo_photo.delay(postponeTime).delay(i * totalPromotionVisibilityTime).fadeIn(slowEffectDuration).delay(initialLabelDelay + (2 * defaultEffectDuration) + labelVisibilityTime).delay(0, function(){ if(i == allPromotions.length - 1){animatePromotions(0);} promo_photo.dequeue();}).fadeOut(defaultEffectDuration);
			promo_label.delay(postponeTime).delay(i * totalPromotionVisibilityTime).delay(initialLabelDelay).slideToggle(defaultEffectDuration).delay(labelVisibilityTime).fadeOut(defaultEffectDuration);	
		});
}

function set_classic_search_prices(){
		if ($("input[name=offer_type_id]:checked").val() == '1'){
			$('.classicSearchBuy').css('display', 'inline');
			$('.classicSearchBuy').removeAttr('disabled');
			$('.classicSearchRent').css('display', 'none');
			$('.classicSearchRent').attr('disabled', 'disabled');
		}
		else if($("input[name=offer_type_id]:checked").val() == '2'){
			$('.classicSearchBuy').css('display', 'none');
			$('.classicSearchBuy').attr('disabled', 'disabled');
			$('.classicSearchRent').css('display', 'inline');
			$('.classicSearchRent').removeAttr('disabled');
		}	
	}
	
function set_furnished(){
	var the_radio_val = $("input[name=offer_type_id]:checked").val();
		jQuery.ajax({data:'offer_type_id=' + the_radio_val, success:function(request){jQuery('#offer_type_furr').html(request);}, type:'post', url:'/search/offer_type_furr'});
	}

function stop_slideshow(){
	var allPhotos = $('.slideshowPhoto');
		allPhotos.each(function(){
			$(this).stop(true, true);
			$(this).css("display", "none");
	});
	jQuery.fx.off = true;
}

function set_photos_switch(){	
	var photo = $("#main_photo");
	$("a[rel='slideshow']").each(function(){
		$(this).attr("rel", $(this).attr("href"));
		$(this).css("cursor", "pointer");
		$(this).removeAttr("href", "#");
		$(this).removeAttr("target");
		$(this).click(function(){		
			stop_slideshow();
			$("#slideshow_photo_holder").css("display", "none");
			$("#main_photo_holder").css("display", "block");
			photo.css("background-image","url(" + $(this).attr("rel") + ")");
		});	
	});
	
/*	photo.mouseenter(function(){
		$('#play_button').css("visibility", "visible");
	});
	$('#play_button').mouseenter(function(){
		$('#play_button').css("visibility", "visible");
	});
	photo.mouseleave(function(){
		$('#play_button').css("visibility", "hidden");
	});
	$(".mainPhoto").click(function(){	
		$('#play_button').css("visibility", "hidden");
		stop_slideshow();
		start_slideshow(0);
	});	
	$('#play_button').click(function(){
		$('#play_button').css("visibility", "hidden");
		stop_slideshow();
		start_slideshow(0);
	});*/
}


function start_slideshow(postponeTime){	
	jQuery.fx.off = false;
	var defaultEffectDuration = 200; // 2/3 sec
	var slowEffectDuration = 400; // 1 sec	
	var photoVisibilityTime = 3000; // 10 sec
	//var all_links = $('#thumbnails').find("a");
	var allPhotos = $('.slideshowPhoto');
	$("#slideshow_photo_holder").css("display", "block");
	$("#main_photo_holder").css("display", "none");
	var totalSlideshowVisibilityTime = slowEffectDuration + photoVisibilityTime + defaultEffectDuration;
	allPhotos.each(function(i){
		$(this).delay(postponeTime).delay(i * totalSlideshowVisibilityTime).fadeIn(slowEffectDuration).delay(defaultEffectDuration + photoVisibilityTime).delay(0, function(){ if(i == allPhotos.length - 1){start_slideshow(0);} $(this).dequeue();}).fadeOut(defaultEffectDuration);
	});	
}



function start_presentation_slideshow(){	
	var delayTime = 60000;
	var fadeoutTime = 600;
	jQuery.fx.off = false;
	if ($("a.prev_page").length > 0){
		$("a.prev_page").attr("alt", "Предишна");
		$("a.prev_page").attr("title", "Предишна");
	}
	if ($("a.next_page").length > 0){
		$("a.next_page").attr("alt", "Следваща");
		$("a.next_page").attr("title", "Следваща");
		$("a.next_page").delay(delayTime).delay(0, function(){window.location=$(this).attr("href");  return false;});
		$("#content").delay(delayTime).fadeOut(fadeoutTime);
	}	
	else if($("a.first_page").length > 0){
		$("a.first_page").delay(delayTime).delay(0, function(){window.location=$(this).attr("href");  return false;});
		$("#content").delay(delayTime).fadeOut(fadeoutTime);		
	}
}


function cleanup_children(scope){
	var parents_selected = $("#"+scope).find('.parentCheck:checked');
	var all_children = $("#"+scope).find('.childCheck');	
	var parents_selected_ids=[];
	if (parents_selected.length > 0){
				parents_selected.each(function(){
					parents_selected_ids.push($(this).val());
				});
	}
		all_children.each(function(){
			if (parents_selected.length > 0){
					var child_class = $(this).attr("class");								
					var child_parent_id = child_class.substring(child_class.indexOf("parent_id") + 10, child_class.length);
					if (child_parent_id === '' || $.inArray(child_parent_id, parents_selected_ids) == -1){
						$(this).parent().css("display", "none");
						$(this).attr('checked', false);
					}
					else{
						$(this).parent().css("display", "block");
					}
			}
			else{
				$(this).parent().css("display", "block");
			}
		});	
}

function toggle_parent_children(scope, parent_id){
	var parent = $("#"+scope).find('#parent_id_' + parent_id);
	var parent_children = $("#"+scope).find('.parent_id_' + parent_id);
	cleanup_children(scope);
	parent_children.each(function(){
			if(parent.attr('checked')){
				$(this).parent().css("display", "block");
				$(this).attr('checked', true);
			}
			else{
				$(this).attr('checked', false);
			}
	});
	
}

function filter_children_by_parent(scope){
	var all_parent_checkboxes = $("#"+scope).find('.parentCheck');
	all_parent_checkboxes.each(function(){
			$(this).click(function(){		
				toggle_parent_children(scope, $(this).val());
			});		
	});
}

function set_observer_select(scope, observer_id){
	//alert("set_observer_select(" + scope +", " +observer_id + ")")
	var the_observer = $("#"+observer_id);
	//the_observer.change(function(){alert("hidden select changed")});
	adapt_content_for_low_res();
	var all_checked_checkboxes_length = $("#"+scope).find(".omega").find('input:checked').length;	
	if (all_checked_checkboxes_length == 1){
		var the_checkbox = $("#"+scope).find(".omega").find('input:checked').first();
		jQuery.ajax({data:'city_id=' + the_checkbox.val(), success:function(request){jQuery('#locations_container').html(request);}, type:'post', url:'/search/get_locations_by_city'});
}
	else{	
		jQuery.ajax({data:'city_id=' + '', success:function(request){jQuery('#locations_container').html(request);}, type:'post', url:'/search/get_locations_by_city'});
	}	
}

function activate_observer_selection(scope, observer_id){
	var all_checkboxes = $("#"+scope).find('input');
	all_checkboxes.each(function(){
			$(this).click(function(){		
				set_observer_select(scope, observer_id);
			});		
	});
	set_observer_select(scope, observer_id);
}



function adapt_content_for_low_res(){
	var windowWidth = $(window).width();
	if (windowWidth < 1015){
		$('body').css({"margin-left": "-135px"});
	}
	else{
		$('body').css({"margin-left": "0"});
		}	
}

$('#content').ready(function(){
	setContentMinHeight();					 
});

$(window).load(function(){
	var body_class = $('body').attr("class");
	if (body_class != "slides_tv"){
		adapt_content_for_low_res();
		setContentMinHeight();
		animatePromotions(0);
	}
});
$(window).resize(function(){
	var body_class = $('body').attr("class");
	if (body_class != "slides_tv"){												
		adapt_content_for_low_res();
	}
});
