window.onload = maxWindow;

function maxWindow()
{
    window.moveTo(0,0);

    if (document.all)
    {
      top.window.resizeTo(screen.availWidth,screen.availHeight);
    }
    else if (document.layers||document.getElementById)
    {
      if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth)
      {
        top.window.outerHeight = screen.availHeight;
        top.window.outerWidth = screen.availWidth;
      }
    }

    //$("#vertical_menu").css("left","132px");
    
    setTimeout("hidemenu();", 500);
        
}

function hidemenu()
{
		$("#vertical_menu").stop().animate({left: '15px'}, 600, function() {
		  $("#menu_box table").hide();
      if (move_click > 0)
        $(".div_link_backward").show();
      else
        $(".div_link_backward").hide();
		});

}

function getWidth()
{
	if (navigator.appName === "Microsoft Internet Explorer")
	{
		return document.body.offsetWidth;
	}
	else
	{
		return window.innerWidth;
	}
}

function getHeight()
{
	if (navigator.appName === "Microsoft Internet Explorer")
	{
		return document.body.offsetHeight;
	}
	else
	{
		return window.innerHeight;
	}
}

function setLayoutDimensionsHome()
{
	global_width = getWidth();
	global_height = getHeight();

  //width of the right div, the one containing the photos
	right_home_width = global_width - left_home_width;

	container_properties = {
		"width" : global_width + "px",
		"height" : global_height + "px"
	};

	left_home_properties = over_left_home_properties = {
		"width" : left_home_width + "px",
		"height" : global_height + "px"
	};

	right_home_properties = {
		"width" : right_home_width + "px",
		"height" : global_height + "px"
	};

	vertical_menu_properties = {
		"height" : global_height + "px"
	};

        $("#table_menu").css("height", global_height + "px");
        $("#menu_box").css("height", global_height + "px");
  
	$("#container").css(container_properties);
	$("#left_home").css(left_home_properties);
	$("#over_left_home").css(left_home_properties);
	$("#right_home").css(right_home_properties);
	$("#vertical_menu").css(vertical_menu_properties);


        if ($('#clientiContainer').length != 0)
        {
            $('.jScrollPaneContainer').css("height", global_height + "px");
            $('.jScrollPaneContainer').css("width", right_home_width + "px");

            $('#clientiContainer').jScrollPane();
        }

        show_move_buttons_menu();

	resizeImagesHome();
}

//ridimensiona le immagini
function resizeImagesHome()
{
	$("#right_home #bghome").each(function(index) {

		//ottieni la nuova width
		var new_img_height = get_new_width(images_width,images_height);

		//ridimensiona l'immagine
		$(this).height(new_img_height);
	});

}

function setLayoutDimensions()
{
    global_width = getWidth();
    global_height = getHeight();

    //width of the right div, the one containing the photos
    right_home_width = global_width - left_home_width;

    container_properties = {
            "width" : global_width + "px",
            "height" : global_height + "px"
    };

    left_home_properties = over_left_home_properties = {
            "width" : left_home_width + "px",
            "height" : global_height + "px"
    };

    right_home_properties = {
            "width" : right_home_width + "px",
            "height" : global_height + "px"
    };

    vertical_menu_properties = {
            "height" : global_height + "px"
    };

    total_width = right_home_width * loaded_number;

    slider_properties = {
            "width" : total_width + "px",
            "height" : global_height + "px"
    };

    slide_item_image_properties = {
            "width" : right_home_width + "px",
            "height" : global_height + "px",
            "padding":"0px"
    };

    work_box_content_properties = {
            "height" : (global_height-27) + "px",
            "opacity" : 0.8
    };

    var centre_coord = Math.round(global_height/2)-35;

    div_link_forward_properties = {
            "bottom" : centre_coord + "px"
    };

    $("#table_menu").css("height", global_height + "px");
    $("#menu_box").css("height", global_height + "px");

    $("#container").css(container_properties);
    $("#left_home").css(left_home_properties);

    $("#over_left_home").css(left_home_properties);

    $("#right_home").css(right_home_properties);
    $("#vertical_menu").css(vertical_menu_properties);
    $("#slider").css(slider_properties);

    //ricalcolo e risetto il margin quando faccio il resize
    marg = (-1)*(move_click)*right_home_width;
    $("#slider").css("margin-left",marg+"px");

    $(".slide_item_image").css(slide_item_image_properties);
    $(".work_box_content").css(work_box_content_properties);
    $(".div_link_forward").css(div_link_forward_properties);
    $(".div_link_backward").css(div_link_forward_properties);

    //ridimensiona le immagini
    $("#over_left_home_inner").css({"margin-top":"0px"});

    if ($('#clientiContainer').length != 0)
    {
        $('.jScrollPaneContainer').css("height", global_height + "px");
        $('.jScrollPaneContainer').css("width", right_home_width + "px");

        /*$('.jScrollPaneTrack').css("height", global_height + "px");
        $('#clientiContainer').css("height", global_height + "px");*/
        $('#clientiContainer').jScrollPane();
    }

    show_move_buttons_menu();
    resizeImages();
}


//setta lo slider per contenere 3 immagini
function setSlider()
{
	//numero immagini caricate
	loaded_number = 3;
	//reset move_click
	move_click = 0;
	
	total_width = right_home_width * loaded_number;

	slider_properties = {
		"width" : total_width + "px",
		"height" : global_height + "px"
	};
	
	$("#slider").css(slider_properties);
	$("#slider").css("margin-left","0px");
	$(".slide_item_image").css(slide_item_image_properties);

}

function setSliderForClienti()
{
	//reset move_click
	move_click = 0;
        loaded_number = 1;

	total_width = right_home_width;

	slider_properties = {
		"width" : total_width + "px",
		"height" : global_height + "px"
	};

	$("#slider").css(slider_properties);
	$("#slider").css("margin-left","0px");
}


//trova la nuova width
function get_new_width(my_width,my_height)
{
	k_w = right_home_width/my_width;
	k_h = global_height/my_height;
	
	if (k_w > k_h)
	{
		return new_img_height = Math.round(k_w * my_height);
	}
	else
	{
		return new_img_height = Math.round(k_h * my_height);
	}
}

//ridimensiona le immagini
function resizeImages()
{
	$("#slider .slide_item_image img").each(function(index) {

		//ottieni la nuova width
		var new_img_height = get_new_width(images_width,images_height);
		
		//ridimensiona l'immagine
		$(this).height(new_img_height);
	});

}

//update the slider
function resetSlider(html)
{
	$("#slider").empty();
	$("#slider").append(html);
	setSlider();
}

//update the slider
function resetSliderForClienti(html)
{
    if ($('#slider').length == 0)
    {
        if ($('#inner_right_home').length == 0)
        {
            $("#right_home").empty();
            $("#right_home").append(html);
        }
        else
        {
            $("#inner_right_home").empty();
            $("#inner_right_home").append(html);


            properties = {
                    "width" : right_home_width + "px",
                    "height" : global_height + "px"
            };

            $("#inner_right_home").css(properties);
            /*$("#inner_right_home").css("margin-left","150px");*/

        }
    }
    else
    {
        $("#slider").empty();
        $("#slider").append(html);
        setSliderForClienti();

        $('.div_link_forward').hide();
        $('.div_link_backward').hide();
        $('.get_more_informations').hide();
    }

    $('#clientiContainer').jScrollPane();

}

function images_in_the_cache(html)
{
	$("#slider").empty();
	var temp_div = $("<div class='temporary'></div>").html(html);
	
	$("body").append(temp_div);
	$(".temporary").css("display","none");

	$(".temporary").find(".slide_item_image").each(function(){
		var this_src = $(this).find("img").attr("src");
		cacheImage = new Image();
		cacheImage.src = this_src;
		cache.push(cacheImage);
	});
	
	$(".temporary").detach();
}

//carica le prime tre immagini
//id_work: tipo_lavoro
function first_images(id_work)
{
	var my_url = base_url+"/works/workview/"+id_work;
	inject_images(my_url);
	
	//apri la curtain del lavoro di default
	var curr_img = get_current();
	if (curr_img != "undef")
	{
		open_curtain(curr_img.attr("id"));
	}
	
	//aggiungine una se trova solo un'immagine
	check_and_eventually_load_next_image();
}

//carica immagini nel div a destra
//my_url:url per il caricamento parziale
function inject_images(my_url)
{
	$.ajax({
		url: my_url,
		async: false,
		dataType: "html",
		success: function(html){
			
			images_in_the_cache(html);
			resetSlider(html);
			resizeImages();
			inject_description();
		}
	});	
}

//carica le prime tre immagini di un lavoro
//id_lavoro: id del lavoro
function first_images_work(id_lavoro)
{
	var my_url = base_url+"/works/workimages/"+id_lavoro;
	inject_images(my_url);

}

function get_images_number()
{
	var temp_img_number = 0;
	
	$(".slide_item_image").each(function(){
		
		temp_img_number++;
		
	});
	
	return temp_img_number;
}

//ottieni l'url dall'id dell'ultima immagine caricata
function get_url_from_last_id()
{
	var next_id = $("#slider").children().last().children().attr("id");
	my_url = base_url+"/works/" + next_image_action + "/" + next_id;
	return my_url;
}

//controlla che più di un'immagine sia stata caricata, in caso contrario caricane un'altra (eventualmente la stessa)
function check_and_eventually_load_next_image()
{
	//aggiungine una se trova solo un'immagine
	var temp_image_numer = get_images_number();
	if (temp_image_numer === 1)
	{
		var next_url = get_url_from_last_id();
		appendNextImage(next_url);
	}
}

//carica le prime tre immagini
function load_images(match_string)
{
	//carica le prime tre immagini
	$(match_string).click(function() {
		var link_url = $(this).attr("href");
		var link_id = $(this).attr("id");
		$.ajax({
			url: link_url,
			async: false,
			success: function(html){
				
				//setto la colonna di sinistra
				set_left_column(link_id,match_string);
				
				images_in_the_cache(html);
				resetSlider(html);
				resizeImages();
				inject_description();
				
				if (match_string === ".work_item_link")
				{
					next_image_action = "worknext";
				}
				else
				{
					next_image_action = "clientenext";
				}
				
				//aggiungine una se trova solo un'immagine
				check_and_eventually_load_next_image();
			
				$("#over_left_home_inner").css({"margin-top":"0px"});
			}
		});
		return false;
	});
}


//riempi la colonna a sinistra
//id: tipo_lavoro o id_cliente
//match_string: per decidere se deve estrarre le thumb di un cliente o di una tipologia di lavoro
function set_left_column(id,match_string)
{
	var action_name = "tipothumb";
	
	if (match_string != ".work_item_link")
	{
		action_name = "clientethumb";
		
	}

	$.ajax({
		url: base_url+"/works/"+action_name+"/"+id,
		async: false,
		dataType: "html",
		success: function(html){
			
			$("#over_left_home_inner").empty();
			$("#over_left_home_inner").append(html);
			
			//apri la prima curtain
			open_curtain($(".thumb_image_box").first().attr("id"));
			
			event_curtains();
		}
	});
}


//carica l'immagine successiva
//myurl: link dal quale ottiene l'immagine successiva
function appendNextImage(my_url)
{
	$.ajax({
		url: my_url,
		async: false,
		success: function(html){
			//aumenta di 1 il conteggio delle immagini caricate
			loaded_number = loaded_number +1;
			//aggiorna la lunghezza dello slider
			total_width = right_home_width * loaded_number;
			
			$("#slider").css("width" , total_width+"px");
			//carico in cache

			//appendo la nuova immagine
			$("#slider").append(html);
			
			//setto la dimensione del div che la contiene
			$(".slide_item_image").css(slide_item_image_properties);
			resizeImages();
			
			$(".div_link_forward").show();
		}
	});
}

//muovi avanti o indietro
//mysign può essere +1 (avanti) o -1 (indietro)
function move_slider(mysign)
{
	//non puoi andare indietro se è la prima slide
	if (mysign === 1 || move_click > 0)
	{
		
		//ottieni la current curtain
		current_curtain = get_current_curtain();
		
		if (mysign === 1)
		{
			next_image = get_next();
		}
		else
		{
			next_image = get_prev();
		}
		
		
		if (current_curtain !== "undef" && next_image !== "undef")
		{
			if (current_curtain.attr("id") !== next_image.attr("id"))
			{
				temp_id_cur = next_image.attr("id");
				open_curtain(temp_id_cur);
				
			}
		}
							
		new_margin = (-1)*(move_click + mysign)*right_home_width;
		move_click = move_click + mysign;
		$("#slider").animate({"margin-left" : new_margin+"px" }, 600, "linear", function() {
			
			inject_description();
			
			//se sto andando avanti
			if (mysign === 1)
			{
				//ottieni l'URL dal quale avere l'immagine successiva
				my_url = get_url_from_last_id();
				appendNextImage(my_url);
			}
			
		});
		  
    if (move_click > 0)
      $(".div_link_backward").show();
    else
      $(".div_link_backward").hide();	
      	
	}
}


//setta l'evento jquery mouseenter
function event_curtains(my_place)
{
	my_place = (typeof my_place == "undefined") ? null : my_place;
	
	$(".thumb_image_box_inner").css('opacity', 0.7);
	
	
	$(".thumb_image_box").mouseenter(function() {
		var id_lavoro_left = $(this).attr("id");
		//apri la tendina
		open_curtain(id_lavoro_left,my_place);
	});
	/*
	$(".thumb_image_box_inner").mouseenter(function() {
		var id_lavoro_left = $(this).parent().attr("id");
		//apri la tendina
		open_curtain(id_lavoro_left,my_place);
	});
  */
}


//apri la tendina a sinistra per un lavoro
function open_curtain(id_lavoro_left,my_place)
{
	my_place = (typeof my_place == "undefined") ? null : my_place;
	
	$(".thumb_image_box").each(function(){
	
		var id_temp = $(this).attr("id");
		
		if (id_temp === id_lavoro_left)
		{
			$(this).find(".thumb_image_box_inner").stop().animate({"height" : "132px","width" : "132px"},600,function(){

				//ottieni la descrizione
				var desc = $(this).parent().find(".thumb_image_description");
				
				//mostra la descrizione
				if (desc.css("display")== "none")
          desc.css({"display":"block"});
				
				desc.find("p").find(".lincato").click(function(){
					
					if (my_place === null)
					{
						first_images_work(id_temp);
						
						//aggiungine una se trova solo un'immagine
						check_and_eventually_load_next_image();
					}
					else
					{
						location.href = base_url + "/works/index/homethumb/"+id_temp;
					}
			
				});  
			});
		}
    else
    {
      $(this).find(".thumb_image_box_inner").stop().animate({"height" : "24px","width" : "24px"},10,function(){});
      
      var desc = $(this).find(".thumb_image_description");
      desc.css({"display":"none"});   
    }				
	});
}


//ritorna l'oggetto DOM tendina corrente
function get_current_curtain()
{
	var temp_curtain = "undef";
	
	$(".thumb_image_box_inner").each(function(){
		
		var temp_width = $(this).width();
		if (temp_width > 100)
		{
			temp_curtain = $(this).parent();
		}
	});
	return temp_curtain;
}

//restituisci l'oggetto DOM del div che contiene l'immagine numero image_number
function get_image(image_number)
{
	temp_image = "undef";
	
	var temp_flag = 0;
	
	$(".slide_item_image").each(function(){
		
		temp_flag++;
		
		if (temp_flag === image_number)
		{
			temp_image = $(this);
		}
		
	});
	
	return temp_image;
}

//restituisci l'oggetto DOM del div che contiene l'immagine corrente
function get_current()
{
	return get_image(move_click+1);
}

//restituisci l'oggetto DOM del div che contiene l'immagine successiva
function get_next()
{
	return get_image(move_click+2);
}

//restituisci l'oggetto DOM del div che contiene l'immagine precedente
function get_prev()
{
	return get_image(move_click);
}

//inserisci la descrizione nel box work_box_content_inner
function inject_description()
{
	var current_img = get_current();
	if (current_img != "undef")
	{
            var temp_content = current_img.find(".item_content").html();
            $(".work_box_content_inner").empty();
            $(".work_box_content_inner").append(temp_content);

            var colore = current_img.find(".item_content").css('background-color');
            $(".work_box_content").css('background-color',colore);
            $(".get_more_informations").css('background-color',colore);
	}
}


//mostra i bottoni per il movimento verticale
function show_move_buttons()
{
	$("#over_left_home").hover(function(){
		if ($(this).find("#over_left_home_inner").height() > global_height)
		{
			$(".go_up").css({"display":"block"});
			$(".go_down").css({"display":"block"});
		}
	},
	function(){
		$(".go_up").css({"display":"none"});
		$(".go_down").css({"display":"none"});
	});
}

function show_move_buttons_menu()
{
	if ($("#contenuti_menu").height() > global_height)
	{
		$(".go_up_menu").css({"display":"block"});
		$(".go_down_menu").css({"display":"block"});
	}
	else
	{
		$(".go_up_menu").css({"display":"none"});
		$(".go_down_menu").css({"display":"none"});
  }
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


function animate_vertical_menu()
{
	//vertical menu visibility toggle
	$("#vertical_menu").hover(function(){
		$(".div_link_backward").hide();
		$("#menu_box table").show();
		$(this).stop().animate({left: '132px'}, 600, function() {});
	},
	function(){
		$(this).stop().animate({left: '15px'}, 600, function() {
		  $("#menu_box table").hide();
      if (move_click > 0)
        $(".div_link_backward").show();
      else
        $(".div_link_backward").hide();
		});
	});
}


//espandi il menà works
function expand_work_voice()
{
	$(".work_click").toggle(function(){
    $(".internal_title").slideToggle("slow");
		return false;
	},
	function(){
		$(".internal_title").slideToggle("slow");
		return false;
	});
}

//espandi il menù works
function expand_clienti_voice()
{
	$(".clienti_click").toggle(function(){
		$(".internal_title_clienti").slideToggle("slow");

                var htmlClienti = getClientiPage();

                resetSliderForClienti(htmlClienti);

                return false;
	},
	function(){
        $(".internal_title_clienti").slideToggle("slow");
		return false;
	});
}

function getClientiPage()
{
    var strClienti = '';

    $.ajax({
            url: '/loghiclientipub/index/',
            async: false,
            success: function(html){
                strClienti = html;
            }
    });

    return strClienti;
}

//slide per le thumbs
function vertical_slide()
{
	$(".go_up").hover(function(){
		$("#over_left_home_inner").stop().animate({"margin-top": "0px"},3000,function(){});
	},
	function(){
		$("#over_left_home_inner").stop();
	});
	
	$(".go_down").hover(function(){
		var box_diff = global_height - 24 - $("#over_left_home_inner").height();
		$("#over_left_home_inner").stop().animate({"margin-top": box_diff+"px"},3000,function(){});
	},
	function(){
		$("#over_left_home_inner").stop();
	});	
}

//slide per le thumbs
function vertical_slide_menu()
{
	$(".go_up_menu").hover(function(){
	   var box_diff = global_height - $("#contenuti_menu").height();
	   var speed =  1000000/box_diff;
		$("#contenuti_menu").stop().animate({"margin-top": "0px"},speed,function(){});
	},
	function(){
		$("#contenuti_menu").stop();
	});
	
	$(".go_down_menu").hover(function(){
		var box_diff = global_height - $("#contenuti_menu").height();
		var speed = 1000000 / (-1 * box_diff);
		
		$("#contenuti_menu").stop().animate({"margin-top": box_diff+"px"},speed,function(){});
	},
	function(){
		$("#contenuti_menu").stop();
	});	
}

function showvideo(video)
{
  $("#video_player").attr("href",video);

  flowplayer("video_player", "/Public/swf/flowplayer-3.2.1.swf");

  $("#click_video_container").show();
  $("#video_container").show();
  $("#pulsante_chiudi_video").show();
}

function hidevideo()
{
  $("#video_container").hide();
  $("#click_video_container").hide();
  $("#pulsante_chiudi_video").hide();
  
  var player = flowplayer("video_player");
  player.close();
}


