/*--------------------------------------------------
P I E T R O F I O R E N T I N I
website by websolute
http://www.websolute.it
--------------------------------------------------*/


/*--------------------------------------------------
login
--------------------------------------------------*/
function fancy_boxes() {
    $("a.login_btn").fancybox({
        'width': 380,
        'height': 245,
        'type': 'iframe'
    });
	$("a.iFramePrivacy").fancybox({
        'width': 520,
        'height': 600,
        'type': 'iframe'
    });
    $("a.iFrameTellToFriend").fancybox({
        'width': 520,
        'height': 500,
        'type': 'iframe'
    });
    $("a.iFrameWeCallYou").fancybox({
        'width': 520,
        'height': 670,
        'type': 'iframe'
    });
    $("a.iFrameEmailUs").fancybox({
        'width': 520,
        'height': 500,
        'type': 'iframe'
    });

    $("a.iFrameRequestaMeeting").fancybox({
        'width': 520,
        'height': 550,
        'type': 'iframe'
    });

    $("a.iFrameRequestCatalog").fancybox({
        'width': 520,
        'height': 500,
        'type': 'iframe'
    });

    $("a.iFrameRequestSpareParts").fancybox({
        'width': 520,
        'height': 580,
        'type': 'iframe'
    });


}




function chooseMercato(co_id, area_id) {
    var url;
    url = "/choose_mercato.aspx?co_id=" + co_id + "&action=set_area" + "&area_id=" + area_id + "&dummy=" + new Date().getTime();
    //url = "/choose_mercato.aspx?co_id=" + co_id + "&action=set_area" + "&area_id=" + area_id;
    window.location.href = url;
}



function chooseLang(co_id, lang_id) {
    var url;
    url = "/choose_lang.aspx?co_id=" + co_id + "&action=set_lang" + "&lang_id=" + lang_id + "&dummy=" + new Date().getTime();
    //url = "/choose_lang.aspx?co_id=" + co_id + "&action=set_lang" + "&lang_id=" + lang_id;
    window.location.href = url;
}





/*--------------------------------------------------
Paging:  Applicazioni
--------------------------------------------------*/
function ChangePageApplication(cur_co_id, app_grup_id, app_id, curPage, direction, WsMethod){
var wsurl;

     wsurl = "/ws/wsGetContent.asmx/" + WsMethod;

     if ( WsMethod == 'GetListApplication'){

       //$('#corpo_prod').hide('slow');

        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{cur_co_id: " + cur_co_id + ",app_grup_id: " + app_grup_id + ",app_id: " + app_id + ",curPage: " + curPage +  ", NavDirection:'" + direction + "'}",
            dataType: "json",
            success: function(data)
            {
               $("#corpo_prod").html(data.d.HTML);

               $('#corpo_prod').show('slow');
            },
            error: function(data)
            {
                if (data.d != null)
                $("#corpo_prod").html(data.d.ErrMessage);
            }
        });

     }

 }


/*--------------------------------------------------
Paging:  News-Eventi-Ricerca
--------------------------------------------------*/
function ChangePage(cur_co_id, curPage, direction, WsMethod){
var wsurl;

    wsurl = "/ws/wsGetContent.asmx/" + WsMethod;

    if ( WsMethod == 'GetListNewsEventi'){

       //$('#corpo_prod').hide('slow');

        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{cur_co_id: " + cur_co_id +  ",curPage: " + curPage +  ", NavDirection:'" + direction + "'}",
            dataType: "json",
            success: function(data)
            {
               $("#corpo_prod").html(data.d.HTML);
               $('#corpo_prod').show('slow');
            },
            error: function(data)
            {
                if (data.d != null)
                $("#corpo_prod").html(data.d.ErrMessage);
            }
        });
     }
 

    if ( WsMethod == 'GetSearchResult' ){
       
        //$('#corpo_prod').hide('slow');

        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{curPage: " + curPage +  ", NavDirection:'" + direction + "'}",
            dataType: "json",
        
            success: function(data)
            { 
               
               $("#corpo_prod").html(data.d.HTML);
               $('#corpo_prod').show('slow');
            },
            error: function(data)
            {
                if (data.d != null)
                    $("#corpo_prod").html(data.d.ErrMessage);
            }
        });
    }





    if (WsMethod == 'GetSearchResultAttach') {

        $this = $(this);
        $.ajax({
            type: "POST",
            url: wsurl,
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{curPage: " + curPage + ", NavDirection:'" + direction + "'}",
            dataType: "json",

            success: function (data) {
                $("#corpo_prod").html(data.d.HTML);
                $('#corpo_prod').show('slow');
            },
            error: function (data) {
                if (data.d != null)
                    $("#corpo_prod").html(data.d.ErrMessage);
            }
        });
    }
    
    

                 
}







/*--------------------------------------------------
	SEARCH
--------------------------------------------------*/
function do_search(){
	var text_to_search;
	
	$("input[id$='button_search']").click(function() {

			text_to_search=$("#text_to_search").val()
			
			if(text_to_search.length >0){
			       //$("body").removeAttr("onunload");
				   //$("#fake_text_to_search").val(text_to_search);
					$("#frm_Search").submit();
			}
			
	});
	
	$("#text_to_search").keyup(function(event){
		if(event.keyCode == 13){
			text_to_search=$("#text_to_search").val()
			if(text_to_search.length >0){
					$("#button_search").click();
			}
		}
	});
  
}








/*--------------------------------------------------
SEARCH ALLEGATI
--------------------------------------------------*/
function do_search_attach() {
    var text_to_search;

    $("input[id$='button_search_attach']").click(function () {

        text_to_search = $("#text_to_search_attach").val()

        if (text_to_search_attach.length > 0) {
            $("#frm_Search_Attach").submit();
        }

    });

    $("#text_to_search_attach").keyup(function (event) {
        if (event.keyCode == 13) {
            text_to_search = $("#text_to_search_attach").val()
            if (text_to_search_attach.length > 0) {
                $("#button_search_attach").click();
            }
        }
    });

}






/*--------------------------------------------------
querystring
--------------------------------------------------*/
function querystring(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(window.location.href);
  if(results == null){
    return "";
	} else {
    return results[1];
	}
}


/*--------------------------------------------------
global
--------------------------------------------------*/
global = {
	menuAlign: function () {
		var menuSize = parseInt($("#menu li a").size());
		var menuWidth = 940;
		var liWidth = 0;
		var extra = 0;
		$("#menu li a:last").css({"margin-right":0});
		liWidth = Math.floor(menuWidth / menuSize) - 22;
		$("#menu li a").css({"width":liWidth + "px"});
		$("#menu li").each(function(){
			extra += $(this).width();
		});
		$("#menu li a:last").css({"width":liWidth + (menuWidth - extra) + "px"});
	},
	focusInput: function () {
		$("input.focus").focus(function(){
			if ($(this).val() == $(this).attr("title")){
				$(this).val("");
			}
		}).blur(function(){
			if ($(this).val() == ""){
				$(this).val($(this).attr("title"));
			}
		});
	},
	menuInt: function () {
		var id = querystring("co_id");
		var $el = $("#menu_int ul li a[href$=co_id=" + id + "]");
		$el.addClass("on");
		$el.parents("ul").show();
		if ($el.next().is("ul")){
			$el.next().show();
		}
		$("#menu_int ul li a[href=#]").click(function(){
			$(this).next().show();
			return false;
		});
		
		
	},
	galMarg: function () {
		$("#gallery li:odd").css({"margin-right":0});
	},
	suggMarg: function () {
		$(".suggerimenti li:last").css({"margin-right":0});
	},
	openTopBar: function () {
		$(".topbar a.paese").not($(".topbar #paese a")).click(function(){
			$("#paese").toggle();
			$("#lingua").hide();
			return false;
		});
		$(".topbar a.lingua").not($(".topbar #lingua a")).click(function(){
			$("#lingua").toggle();
			$("#paese").hide();
			return false;
		});
		$("#header").click(function(){
			$("#lingua").hide();
			$("#paese").hide();
		});
	},
	boxContatti: function () {
		if ($("#right .col .box_contatti").size()) {
			var tit = $("#right .col .box_contatti h2").text();
			$("#right .col .box_contatti").parent().removeClass("col").addClass("box").find(".box_contatti").removeClass("box_contatti").addClass("box_wrap").find("ul").addClass("ico").prev().replaceWith("<h3>" + tit + "</h3>");
		}
	}
}


/*--------------------------------------------------
showreel homepage
--------------------------------------------------*/
function showreelHome() {
	if ($(".showreel").size()){
		$(".showreel").addClass("showreel_" + $("#submenu li").size());
		$("#submenu ul ul").remove();
		$("#submenu a:last").addClass("on");
		
		$("#submenu ul li").each(function(i){
			$(this).addClass("submenu_"+i);
		});
		
		// var
		var looppa;
		var speed = 2000;
		var timeout = 4000;
		var i = 0;
		var size = $(".showreel_img").size();
		var ind;
		var hover = false;
		
		// sistemo le immagini
		$(".showreel_img").each(function(i){
			$(this).css({"background":"url(" + $(this).find("img").attr("src") + ") no-repeat center center", "z-index":(i+1)}).attr("rel",(i+1)).find("img").hide();
		});
		
		// rollover
		function loop(){
			$("#submenu a.on").removeClass("on");
			$("#submenu a:eq(" + (i%4) + ")").addClass("on");
			//self.status = (i%3);
			
			size = size + 1;
			if (hover) {
				$(".showreel_img:eq(" + i + ")").hide().css({"z-index":(size)}).fadeIn(speed/4);
			} else {
				$(".showreel_img:eq(" + i + ")").hide().css({"z-index":(size)}).fadeIn(speed);
			}
			if (i >= $(".showreel_img").size() - 1) {
				i = 0;
			} else {
				i = i+1;
			}
		}
		
		// rollover
		$("#submenu a").mouseover(function(){
			hover = true;
			clearInterval (myloop);
			i = $("#submenu a").index($(this));
			loop(i);
		}).mouseout(function(){
			hover = false;
			myloop = setInterval(function(){
				loop();
			},timeout);
		});
		
		myloop = setInterval(function(){
			loop();
		},timeout);
		
	}
}


/*--------------------------------------------------
tab link
--------------------------------------------------*/
function tab_links(){
	if ($(".tab_links").size()){
		$(".tab_links .tab_bot").hide();
		$(".tab_links .tab").click(function(){
			$(".tab_links .on").removeClass("on");
			$(this).addClass("on");
			$(".tab_links .tab_bot").hide();
			$(".tab_links .tab_bot[id=" + $(this).attr("href").split("#")[1] + "]").show();
			return false;
		});
		$(".tab_links .tab:eq(0)").click();
	}
}


/*--------------------------------------------------
help
--------------------------------------------------*/
function help(){
	var o = false;
	if ($("#left .help_wrap").size()){
		$("#left .help_wrap").hover(function(){
			$(this).find("em").css({"color":"#018B73"});
		},function(){
			$(this).find("em").css({"color":"#454545"});
		});
		if (!o){
			$("#left .help_wrap").click(function(){
				o = true;
				$(this).find("em").css({"color":"#018B73"});
				$(this).css({"cursor":"default"});
				$(this).find(".help_1").slideDown(500);
				$(this).find(".help_2").slideDown(500);
				$(this).find(".links").slideDown(500);
			});
		}
	}
}


/*--------------------------------------------------
gallery
--------------------------------------------------*/
function prodGal(){
	$("#gallery ul li a").click(function(){
		img = $(this).attr("href");
		$("#prod_img").css({"background":"url(" + $("#prod_img img").attr("src") + ") no-repeat 0 0", width:$("#prod_img img").width(), height:$("#prod_img img").height()}).find("img").hide();
		$("#prod_img").append("<div class='loading' />");
		$.ajax({
			cache:false,
			url:img,
			success:loadOut,
			error:loadOut
		});
		return false;
	});
	
	function loadOut(){
		$("#prod_img img").attr("src","http://" + location.hostname + img).fadeIn(500,function(){
			$("#prod_img").animate({"height":$("#prod_img img").height()},200);
		});
		ciccio = setTimeout(function(){
			$("#prod_img .loading").fadeOut(300,function(){
				$(this).remove();
			});
		},300);		
	}
}

/*--------------------------------------------------
activemenu--------------------------------------------------*/
function activemenu (){
	var id = querystring("co_id");
	//alert (id);
	
	try {
		if (id != 1){
			var menuOn = $("#path li:eq(2) a").attr("href").split("=")[1];
			$("#menu ul li a[href$=" + menuOn + "]").addClass("on");
			
		} else {
			$("#menu ul li:eq(0) a").addClass("on");
		}
	} catch(err) {
	  //
	}
}

/*--------------------------------------------------
INIT
--------------------------------------------------*/
$(function () {
	try {
		$("#video_scroll a.prev").addClass("disabled");	
		$(".scrollable").scrollable();
		$("#video_scroll .prev, #video_scroll .next").live("click",function(){
			$(".player").jfe_restart();
		});
		$("#prefooter_wrap ul:eq(0)").hide();
		$("#path ul li:eq(1)").hide();
		$(".mappa ul li:eq(0)").hide();
		$(".mappa ul li:eq(1)").hide();
		$(".mappa ul:eq(0)").hide();
	} catch(err) {
	  //
	}
    global.menuAlign();
    global.focusInput();
    global.menuInt();
    global.galMarg();
    global.suggMarg();
    global.openTopBar();
    global.boxContatti();
		activemenu();
    showreelHome();
    fancy_boxes();
    do_search();
    tab_links();
    help();
		prodGal();
		$(".player").jfe({ver:"10,0,0", display:"alternate", manual:true, noflash:false});
		$(".mappaFlash").jfe({ver:"10,0,0", manual:false, noflash:false});
	
	//$("#prefooter_wrap ul:eq(1)")find("li").hide();
	
});

