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_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 stop_slideshow(){
	var allPhotos = $('.slideshowPhoto');
		allPhotos.each(function(){
			$(this).stop(true, true);
			$(this).css("display", "none");
	});
	jQuery.fx.off = true;
}

function cleanup_children(scope){
	var parents_selected = $("#"+scope).find('.parentCheck:checked');
	var all_children = $("#"+scope).find('.childCheck');	
	var parents_selected_ids = new Array();
	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.length-1, child_class.length);
					if (child_parent_id == "d" || $.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 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(){
	adapt_content_for_low_res();
	setContentMinHeight();
	animatePromotions(0);
});
$(window).resize(function(){
	adapt_content_for_low_res();
});