window.onresize = realTimeAdjustment;
window.onload = readyToExecute;
jQuery(document).ready(readyToExecute);

function readyToExecute(){
	
	setAutoloader('imageautoadjust');
	realTimeAdjustment();
	start_slideshow();
}

//////////////

function getDimensions()
{
	//create object to be returned
	var dimensions = {width: 0, height: 0};
	
	//our minimum width is 1024 minus scrollbar so...
	var min_width = 1000;
	
	if(window["max_width"])
	{
		//there is a max width defined
		max_width = 1260;
		$('#home_content_wrapper').css('width',window["max_width"]+'px');
		if($('body').innerWidth()>window["max_width"])
		{
			$('#home_content_wrapper').css('width',window["max_width"]+'px');
		}
		else
		{
			if($('body').innerWidth()<min_width)
			{
				$('#home_content_wrapper').css('width',min_width+'px');
			}
			else
			{
				$('#home_content_wrapper').css('width',$('body').innerWidth()+'px');
			}
		}
	}
	else
	{
		//there isn't a max width defined
		if($('body').innerWidth()<min_width)
		{
			$('#home_content_wrapper').css('width',min_width+'px');
		}
		else
		{
			$('#home_content_wrapper').css('width',$('body').innerWidth()+'px');
		}
	}
	
	//return calculated dimensions
	dimensions.width = $('#home_content_wrapper').width();
	dimensions.height = $('#home_content_wrapper').height();
	
	return dimensions;
}

var filled=false;
function realTimeAdjustment() {
	//if(window["superadmin"]) $('#search_string').attr('value','width: '+$('body').innerWidth()+' px');
	
	
	var dimensions = getDimensions();
	
	var margin = 10;
	var maxW = 480;
	var minW = 250;
	var proportion = 1.25;
	var idealW = Math.floor((maxW*2 + minW) / 3);
	idealW = 300;
	var space = dimensions.width - ($('#rightmenu').width() + 10);
	space = space - margin;
	var n = Math.floor(space/idealW);
	if (n<2){
		n = 2;
		idealW = Math.floor(space/n);
	}
	var dif = space%idealW;
	
	var newDif = Math.floor(dif/n);
	var newWidth = idealW+newDif;
	if(newWidth > maxW){
		n++;	
		newWidth = Math.floor(space/n);
	}
	if(newWidth > maxW) newWidth = maxW;
	if(newWidth<minW) newWidth = minW;


	newWidth = newWidth - margin;
	newHeight = newWidth / proportion;
	debug=false;
	
	//fill the rest of the remaining squares
	total_squares = $('#pipeline_wrapper .bottom').length;
	if($('.first_block').is(".ms_doublew")) total_squares += 1;
	if($('.first_block').is(".ms_triplew")) total_squares += 2;
	if($('.first_block').is(".ms_doubleh")) total_squares += 1;
	if($('.first_block').is(".ms_tripleh")) total_squares += 2;
	complete_rows = Math.floor(total_squares/n);
	total_squares_complete_rows = complete_rows*n;
	squares_left = total_squares - total_squares_complete_rows;
	spaces_left = n - squares_left;
	if(!filled){
		hp_fill_pipeline_list(spaces_left);
		filled = true;
	}

	if(debug)
	{
		
		console.clear();
		total_squares = 20;
		if($('#stickychild0').is(".ms_doublew")) total_squares += 1;
		if($('#stickychild0').is(".ms_triplew")) total_squares += 2;
		if($('#stickychild0').is(".ms_doubleh")) total_squares += 1;
		if($('#stickychild0').is(".ms_tripleh")) total_squares += 2;
	
		console.log("number of squares per row: " + n);
		
		complete_rows = Math.floor(total_squares/n);
		console.log("complete rows: " + complete_rows);
		
		total_squares_complete_rows = complete_rows*n;
		console.log("number of squares in complete rows: " + total_squares_complete_rows);
		
		squares_left = total_squares - total_squares_complete_rows;
		console.log("squares left: " + squares_left);
		
		spaces_left = n - squares_left;
		console.log("spaces to fill: " + spaces_left);
	}
	
	//resize squares
	//$('#pipeline_wrapper .element').css('width', newWidth + 'px');
	//$('#pipeline_wrapper .element').css('height', newHeight + 'px');
	newHeight = newWidth;
	//all color hovers
	$('div[id^="top_hover"]').width(newWidth).height(newHeight);
	//all hover borders
	$('.pipeline_border').width(newWidth).height(newHeight);
	$.each($('#pipeline_wrapper .element'),function(){
		//check if the first block is double or triple
		//WIDTHS
		//double width
		$('.ms_doublew').css('width', (newWidth * 2 + margin)   + 'px');
		//triple width
		$('.ms_triplew').css('width', (newWidth * 3 + margin*2)   + 'px');

		$(this).width(newWidth).height(newHeight);
		bottomHeight = $(this).children('.top_size').height();
		if($.browser.msie){  //IE7 FIX!!!
			var version = parseInt($.browser.version, 10);
			if(version<8){
				$(this).children('.bottom').height(newHeight-(bottomHeight-newHeight)-2);
			}
			
		}else{
			$(this).children('.bottom').height(newHeight-bottomHeight);
		}
		$(this).children('.visual_content').children('.bottom').height(newHeight); //this one is for the slideshow

		
		//special slideshow
		$(this).children('.image_overlay').width(newWidth);
		$(this).children('.controller_space').width(newWidth);
		$(this).children('.image_overlay').css('margin-top',$(this).children('.controller_space').height()+'px');

		//community dashboard
		$(this).children('.dashboard').width(newWidth-10);

		//encyclopedia
		$(this).children('.yellow_hover').width(newWidth-20).height(newHeight-bottomHeight-20);


		//news comment counter
		//$(this).children('.hover_text').children('.comments_count').css('margin-top',(newHeight-$(this).children('.hover_text').children('.marginer').height()-$(this).children('.hover_text').children('.comments_count').height()-10)+'px');

		$('.find_any_car').css('margin-left', ($('.logo').width()+$('.main_menu').width()-160)+'px');
	});
	

	$('.hd_text').css('width',$('body').innerWidth()+'px');
	$('#search_string').css('width','310px');
	stretch = ($('body').innerWidth()-1000)+210;
	if(stretch<=310 && stretch>=255)
	{
		$('#search_string').css('width',stretch+'px');	
	}


	
	
	$('#pipeline_wrapper').css('width', (dimensions.width - $('#rightmenu').width() - 20) + 'px');

	debug=false;
	if(debug)
	{
		console.log("rightmenu - width: "+$('#rightmenu').width());
		console.log("wrapper - width: "+$('#wrapper').width());
		console.log("dimensions - width: "+dimensions.width);
		console.log("dimensions - height: "+dimensions.height);
		console.log("rightmenu - height: "+$('#rightmenu').height());
		console.log("wrapper - height: "+$('#wrapper').height());
	}
	
	//exchange the extra image in the special block
	if($('.first_block').width()<600){
		$('#small_extra').show();
		$('#big_extra').hide();
	}else{
		$('#small_extra').hide();
		$('#big_extra').show();
	}

	adjustImageToFrame();
	
	
}

function pipeline_reveal_rest(id, hide_top){
	//community
	$('#element'+id).children('.dashboard').show();
	$('#element'+id).children('.community_bg').addClass('community_bg_hover');

	//encyclopedia
	$('#element'+id).children('.encyclopedia_bg').addClass('encyclopedia_bg_hover');
	$('#element'+id).children('.encyclopedia_bg').removeClass('encyclopedia_bg');
	$('#element'+id).children('.yellow_hover').show();

	$('#border_hover'+id).show();
	if(hide_top) $('#top_unhover'+id+' .top').css('visibility','hidden');
	$('#top_hover'+id).show();
	$('#element'+id).mouseleave(function(){
		//community
		$('#element'+id).children('.dashboard').hide();
		$('#element'+id).children('.community_bg').removeClass('community_bg_hover');

		//encyclopedia
		$('#element'+id).children('.encyclopedia_bg_hover').addClass('encyclopedia_bg');
		$('#element'+id).children('.encyclopedia_bg').removeClass('encyclopedia_bg_hover');
		$('#element'+id).children('.yellow_hover').hide();

		$('#border_hover'+id).hide();
		$('#top_unhover'+id+' .top').css('visibility','visible');
		$('#top_hover'+id).hide();
		$('#element').unbind('mouseleave');
	});
}

function gotoPost(url)
{
	document.location.href=url;	
}

function pipelineMouseOverOut(mo,sticky,background_element,cut_element,full_element)
{
	if(mo==1)
	{
		$('#'+background_element).removeClass();
		$('#'+background_element).addClass('home_rollover_block');
		$('#'+background_element).addClass('pipeline_label_text');		
		$('#'+cut_element).hide();
		$('#'+full_element).show();
	}
	else
	{
		$('#'+background_element).removeClass();
		$('#'+background_element).addClass('pipeline_label_text');
		if(sticky==1)
			$('#'+background_element).addClass('home_sticky_block');
		else
			$('#'+background_element).addClass('home_normal_block');
		$('#'+cut_element).show();
		$('#'+full_element).hide();
	}
}

function mouseOverSpecial(over){
	if($('.first_block').width()<600){
		if(over){
			$('#stickyimg0').attr('src',big_img2);
			$('#xtraimg_small').attr('src',xtrasmall_img2);
		}else{
			$('#stickyimg0').attr('src',big_img1);
			$('#xtraimg_small').attr('src',xtrasmall_img1);
		}
	}else{
		if(over){
			$('#stickyimg0').attr('src',big_img2);
			$('#xtraimg_big').attr('src',xtra_img2);
		}else{
			$('#stickyimg0').attr('src',big_img1);
			$('#xtraimg_big').attr('src',xtra_img1);
		}
	}
	realTimeAdjustment();
}

var pipeline_page_count = 0;
function hp_more_pipeline_list(){
	more = $("#pipeline_more").html();
	$("#pipeline_more").fadeOut('fast',function(){
		$(this).remove();
		feeds_old=$("#pipeline_wrapper").html();
		$("#pipeline_wrapper").append("<center>"+img_loader(17)+"</center>");
		pipeline_page_count++;
		$.get("/ax_pipeline_more.php", { pipeline_page_count: pipeline_page_count},
			function(data){
				$("#pipeline_wrapper").html(feeds_old);
				$("#pipeline_wrapper").append(data);
				$("#pipeline_wrapper").append('<div id="pipeline_more">'+more+'</div>');
				realTimeAdjustment();
				setAutoloader('imageautoadjust');
		});
	});

}


function hp_fill_pipeline_list(n){
	more = $("#pipeline_more").html();
	$("#pipeline_more").remove();

	feeds_old=$("#pipeline_wrapper").html();
	$("#pipeline_wrapper").append("<center>"+img_loader(17)+"</center>");
	$.get("/ax_pipeline_fill.php", {begin:(pipeline_page_count+12), number_fill: n},
		function(data){

			$("#pipeline_wrapper").html(feeds_old);
			$("#pipeline_wrapper").append(data);
			$("#pipeline_wrapper").append('<div id="pipeline_more">'+more+'</div>');

			$('.pipeline_child').mouseenter(function(){
				$(this).children('.pipeline_label_text').children('#cut_title').hide();
				$(this).children('.pipeline_label_text').children('#full_title').show();
				$.each($(this).children('#home_hover_child'),function(){
					$(this).show();
				});
			});

			$('.pipeline_child').mouseleave(function(){
				$(this).children('.pipeline_label_text').children('#cut_title').show();
				$(this).children('.pipeline_label_text').children('#full_title').hide();
				$.each($(this).children('#home_hover_child'),function(){
					$(this).hide();
				});
			});
			realTimeAdjustment();

			id="new"+(pipeline_page_count+12)+"child";
			$('div[id^="'+id+'"]').show(0,function(){adjustImageToFrame()});

		});
}


var slide_locked = false;
function pipeline_special_load(id,user_triggered){
	//we are only checkin if the main image is loaded due to a JQuery bug. Otherwise it will not work on chrome, opera and safari 
	//onload the bindings to avoid "too much recursion" exception
	$('#main_image').unbind('load');

	if(!slide_locked){
		if(user_triggered) clearInterval(si_slideshow);
		$('.button_sel').removeClass('button_sel');
		$('#pipeline_button'+id).addClass('button_sel');

		$('#main_image').load(function(){
			show_visual(id);
		});	
		
		//fadeout images and load new ones
		$('.visual_content').fadeOut(250,function(){
			$('#main_image').attr('src', special_photos[id][0]);
			$('#image_overlay').attr('src', special_photos[id][1]);
		});

		slide_locked = true;
	}
}

function show_visual(id){
	if(special_photos[id][2]!=''){
		$('.visual_content').removeAttr('onclick');
		$('.visual_content').click(function(){
			document.location.href=special_photos[id][2];
		});
		$('.visual_content').css('cursor','pointer');
	}else{
		$('.visual_content').removeAttr('onclick');
		$('.visual_content').unbind('click');
		$('.visual_content').css('cursor','default');
	}
	$('.visual_content').fadeIn(250);
	adjustImageToFrame();
	slide_locked = false;
}


var slideshow_started = false;
var si_slideshow;
var special_photos=Array();
function start_slideshow(){
	if(special_photos.length && !slideshow_started){
		var imgs = '';
		for(i=0;i<special_photos.length;i++){
			imgs += special_photos[i][0]+','+special_photos[i][1];
		}
		jQuery.preloadImages(imgs);
		si_slideshow = setInterval("change_slide()", 5000);
		slideshow_started = true;
	}
}


var current_slide = 1;
function change_slide(){
	if(current_slide==special_photos.length) current_slide = 0;
	pipeline_special_load(current_slide,false);
	current_slide++;
}

function mouseover_load_more(){
	$('.load_more').addClass('load_more_hover');
	$('.load_more').mouseleave(function(){
		$('.load_more').removeClass('load_more_hover');
		$('.load_more').unbind('mouseleave');
	});
}



