﻿/// <reference path="./jQuery/jquery-1.4.1-vsdoc.js" />

function ReadyFn() {

	ie7orLess = isIe7orLess();
	//alert(ie7orLess);

	// carico i box dinamici...
	if (CurrentLang == null || CurrentLang == undefined)
	    CurrentLang = 'it';

	$("#container_attiva").load('/' + CurrentLang + '/_pages/MenuAzienda.asp #box_menu1');
	$("#container_marchio").load('/' + CurrentLang + '/pub/marchi.asp?caller=jquery&previewmenu=12 #box_menu2');
	$("#container_prodotto").load('/' + CurrentLang + '/pub/prodotti.asp?caller=jquery&previewmenu=12 #box_menu3');
	$("#container_myattiva").load('/' + CurrentLang + '/_pages/MenuMyAttiva.asp #box_menu4');
	
	
	$("#container_myattiva").mouseenter(function(){
		init_cleardefaultpage();
	});

	VoceAttiva = $("#menu_top .active");	
		    
	$("#menu1 .tab_central").mouseenter(function() {
	  OpenRoll(1);
	}).mouseleave(function() {
	  SetClosing(1);
	});
	
	$("#menu2 .tab_central").mouseenter(function() {
	  OpenRoll(2);
	}).mouseleave(function() {
	  SetClosing(2);
	});
	
	$("#menu3 .tab_central").mouseenter(function() {
	  OpenRoll(3);
	}).mouseleave(function() {
	  SetClosing(3);
	});
		
	$("#menu4 .tab_central").mouseenter(function() {
	  OpenRoll(4);
	}).mouseleave(function() {
	  SetClosing(4);
	});
   
   
   SetSlideBannerImage();
   
   if(isiPad){ //CONTROLLO PER IPAD
		//SISTEMO IL MENU PER L'IPAD
		$("#menu_top .tabcentral a").attr("href", "javascript:void(0);");
		$("#main_fader").mouseover(function() {
			SetClosing(1);
			SetClosing(2);
			SetClosing(3);
			SetClosing(4);
		});
		//SISTEMO IL FLASH
		$("embed").each(function(){
			var numLow = 4000;
			var numHigh = 6000;
			var timer = Math.floor(Math.random()*(numHigh-numLow+1)) + numLow;
			if($(this).attr("type")=="application/x-shockwave-flash"){
				var arrSrc = $(this).attr("src").split("/");
				if(arrSrc[1]=="swf"){
					var jContent = $(this).parent();
					SwfImage(jContent, arrSrc, 1, timer);
				}
			}
		});
   }
   

   
}
function SwfImage(jContent, arrSrc, numImage, secondtime){
	var strFile = "/" + arrSrc[1] + "/" + arrSrc[2] + "/" + arrSrc[2] + "_shot" + numImage +".jpg";
	var img = new Image();
	$(img).load(function () {
	    $(this).hide();
	    $(this).css("position", "absolute");
	    $(jContent).find("embed").remove();
	    $(jContent).children().slideToggle(1000, function(){$(this).remove();});
	    $(jContent).append(this);
	    $(this).slideToggle(1000);
		$(this).click(function() {					
		});	
		timerImage = setTimeout(function(){SwfImage(jContent, arrSrc, numImage+1, secondtime);}, secondtime);
	}).error(function () {
	    // notify the user that the image could not be loaded
	    timerImage = setTimeout(function(){SwfImage(jContent, arrSrc, 1, secondtime);}, secondtime);
	}).attr({
	    src: strFile,
	    id: "swfimage_shot" + numImage
	});
}

function SetSlideBannerImage(){
	$(".slideBannerImage").each(function(){
		var numLow = 4000;
		var numHigh = 7000;
		var timer = Math.floor(Math.random()*(numHigh-numLow+1)) + numLow;
		var jContent = $(this).find(".contentImage");
		var jFiles = $(this).find(".contentFiles").html();
		$(this).find(".contentFiles").css("display", "none");
		var type = jQuery.trim($(this).attr("class").replace("slideBannerImage", "")).replace("effect_", "");
		
		//timerImage = setTimeout(function(){SlideBannerImage(jContent, jFiles, timer, type);}, timer);
		$(jContent).find("img").remove();
		SlideBannerImage(jContent, jFiles, timer, type);
	});
}
function SlideBannerImage(jContent, filelist, secondtime, type){
	var arrFiles = filelist.split("||");
	var currentfile = $(jContent).find("img").attr("src");
	var nextfile = arrFiles[1];
	for(i=1;i<arrFiles.length;i++){
		if(jQuery.trim(arrFiles[i])==jQuery.trim(currentfile))
		{
			if(i+1<arrFiles.length)
				nextfile = arrFiles[i+1];
			else
				nextfile = arrFiles[1];
			break;
		}
	}
	var img = new Image();
	$(img).load(function () {
	    $(this).hide();
	    $(this).css("position", "absolute");
	    if(type=="fade")
			$(jContent).children().fadeToggle(1000, 'linear', function(){$(this).remove();});
		else if(type=="slide")
			$(jContent).children().slideToggle(1000, 'swing', function(){$(this).remove();});
		$(jContent).append(this);
		if(type=="fade")
			$(this).fadeToggle(1000, 'linear');
		else
			$(this).slideToggle(1000, 'swing');
		$(this).click(function() {					
		});	
		timerImage = setTimeout(function(){SlideBannerImage(jContent, filelist, secondtime, type);}, secondtime);
	}).error(function () {
	    // notify the user that the image could not be loaded
	    $(jContent).find("img").attr("src", arrFiles[1]);
	    timerImage = setTimeout(function(){SlideBannerImage(jContent, filelist, secondtime, type);}, secondtime);
	}).attr({
	    src: nextfile,
	    id: "swfimage_shot"
	});
}

// Browser Detection
var ie7orLess;
var isiPad = navigator.userAgent.match(/iPad/i) != null;

function isIe7orLess() {
    var ret = "";
    var sBrowser = navigator.userAgent;
    if (sBrowser.toLowerCase().indexOf('msie') > -1) 
    {
        var ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
        if (ver <= 7)
            return true;
    }
    return false;
}
// funzioni menù rollover
function OpenRoll(id){
	if((IDOpen!=null) && (IDOpen!=id))
		CloseRoll(IDOpen);
	ClearClosing();
	if((id!=IDOpen) || (IDOpen==null))
		HeightMain = $("#main").height();
	HeightFader = HeightMain
	if($("#box_menu" + id).height()>HeightMain){
		$("#main").css("height", $("#box_menu" + id).height() + "px");
		HeightFader = $("#box_menu" + id).height();
	}
	
        $("#main_fader").css("height", HeightFader + "px");
        //$("#main_fader").css("top", "-" + $("#main").height() + "px");
    
    //$('#main').parent().css("height", $('#main').height() + "px");
    //$("#box_menu" + id).animate({ height: 512 });
    $("#box_menu" + id).css("display", "inline");
    //$("#box_menu" + id).slideDown(500);
    $("#menu" + id).addClass("selected");	
        
    VoceAttiva.removeClass("active");
        
    $("#box_menu" + id).unbind('mouseenter').mouseenter(function() {
	  OpenRoll(id);
	}).unbind('mouseleave').mouseleave(function() {
	  SetClosing(id);
	});
	
	IDOpen = id;
}
function CloseRoll(id){
	$("#box_menu" + id).css("display", "none");
	//$("#box_menu" + id).slideUp(300);
    $("#menu" + id).removeClass("selected");
    
    VoceAttiva.addClass("active");
    
	$("#main_fader").css("height", "0px");
	//$("#main").css("height", HeightMain + "px");
	$("#main").css("height", "auto");
	if(IDOpen==id)
		IDOpen = null;
}
var IDOpen = null;
var HeightMain = null;
var ToggleClosing = null;
var VoceAttiva = null;
function SetClosing(id) {
    ToggleClosing = setTimeout("CloseRoll(" + id + ");", 150);
}
function ClearClosing() {
    if (ToggleClosing != null){
        clearTimeout(ToggleClosing);
        ToggleClosing = null;
    }
}


var whiteList = new Array();

//
// READY:
$(document).ready(function(){

    ReadyFn();
    $.SelectField.SetValueSelectOption();
    //$("input:button").each(function(){
    
});


var showGenericTagDiv = function (divName) {
	showTagDiv (divName, 'GenericTabPanel');
	if (document.forms['frmPostBack']) {
		if (document.forms['frmPostBack'].ActiveTabPanel) {
			document.forms['frmPostBack'].ActiveTabPanel.value = divName;
		}
    }
}

var showTagDiv = function (divName, divTabPanel) {

    $("#"+divTabPanel).find("#Tabs").find("div").each(function () {
        //$(this).addClass('TabUnselected');
        if ($(this).attr('id') == ("Tab" + divName))
            $(this).addClass('selected');
        else
            $(this).removeClass('selected');
    });
    $("#"+divTabPanel).find("#Panels").children("div").each(function () {
        if ($(this).attr('id') == ("Div" + divName)) {
            $(this).css('display', 'block');
        } else {
            $(this).css('display', 'none');
        }
    });

    $("."+divTabPanel+"_OTHER").children("div").each(function () {
        if ($(this).attr('class') == ("divAltro" + divName)) {
              $(this).css('display', 'block');
        } else {
            $(this).css('display', 'none');
        }
    });
    
    /*var tb = document.getElementById(divTabPanel+'Select');
    tb.value = divName;*/
}

var RunFieldRadio = function () {

    $(".field_radio").find("input").each(function () {

        if ($(this).is(':checked')) {
            $(this).parent().find('.skinned-radio').each(function () {
                $(this).addClass('radio-checked');

            });
        } else {
            $(this).parent().find('.skinned-radio').each(function () {
                $(this).removeClass('radio-checked');
            });
        }

    });

};

var RunFieldFile = function () {

    $(".field_file").find("input").each(function () {
		//alert($(this).val());
		var valueinsert = $(this).val();
        $(this).parent().find('.skinned-filename').each(function () {
            $(this).html(valueinsert);
        });
    });

};
var setFieldSelect = function(obj){
	if(typeof(obj)=="object"){
		$(obj).find("option:selected").each(function () {
		    $(obj).parent().find(".field_value").text($(this).text());     
		});
    }else{
		$(".field_inner select").each(function () {
			setFieldSelect(this);
			$(this).unbind('change').change(function() {
			  setFieldSelect(this);
			});
		});
    }
};
(function ($) {
    $.SelectField = {
        GetSelectOptions: function (obj, e) {
            $(obj).parent().find('.select_options').each(function () {
                $(this).slideToggle(100);
                $(this).mouseleave(function () {
                    $(this).slideUp(300);
                });
            });
        },

        SetSelectOptions: function (obj) {
            var txtselect = "";
            var valueselect = "";
            valueselect = $(obj).attr("name");
            $.SelectField.GetSelectOptions($(obj).parent().parent().parent());
            $(obj).parent().parent().parent().parent().parent().find('select').each(function () {
                $(this).val(valueselect);
            });
            $.SelectField.SetValueSelectOption();
        },

        SetValueSelectOption: function () {
            $(".field_select select option:selected").each(function () {
                var txtselect = $(this).text();
                var valueselect = $(this).attr("value");
                $(this).parent().parent().find('.select_header span').each(function () {
					if(txtselect=="[CUSTOMINSERT]")
						$(this).html("Altro...");
					else
						$(this).html(txtselect);
                });
                $(this).parent().parent().find('a').each(function () {
                    $(this).removeClass("selected");
                });
                $(this).parent().parent().find('.select_options a[name$=\'' + valueselect + '\']').each(function () {
                    $(this).addClass("selected");
                });
            });
        },
        CheckValueOther: function(obj) {
			$(obj).parent().parent().parent().parent().parent().find('select').each(function () {
                if($(this).val()=="[other]"){
					$(this).parent().parent().find(".field_other_text").find("input").each(function(){
						var textaltro = "specificare altro...";
						$(this).val(textaltro);
						$(this).click(function(){
							if($(this).val()==textaltro)
								$(this).val("");
						});
						$(this).parent().parent().css("display", "inline");
						$(this).parent().parent().css("visibility", "visible");
					});
                }else{
					$(this).parent().parent().find(".field_other_text").find("input").each(function(){
						$(this).parent().parent().css("display", "none");
						$(this).parent().parent().css("visibility", "hidden");
					});
                }
            });
        },
		CustomValue: null,
        ChangeValue: function(obj) {
			$(obj).parent().parent().parent().find('select').each(function () {
				$(this).find("option[value='" + this.CustomValue + "']").remove();
				$(this).append('<option value="'+$(obj).val()+'" selected>[CUSTOMINSERT]</option>');
				this.CustomValue = $(obj).val();
            });
        },
        SetSelected: function(valore, name){
			if(valore!=""){
				$("#" + name).each(function () {
					var impostato = "";
					$(this).find("option").each(function () {
						if($(this).val()==valore){
							$(this).parent().val(valore);
							impostato = valore;
						}
					});
					if(impostato==""){
						$(this).append('<option value="'+valore+'" selected>[CUSTOMINSERT]</option>');
						$(this).parent().parent().find(".field_other_text").find("input").each(function(){
							
							$(this).val(valore);
							$(this).parent().parent().css("display", "inline");
							$(this).parent().parent().css("visibility", "visible");
							$(this).parent().parent().parent().find('.select_header span').each(function () {
							    $(this).html(valore);
							});
						});
					}
				});
			}
        }
        
    };
})(jQuery);

$(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$( "#dialog_alert_message" ).dialog({
		modal: true,		autoOpen: true,		dialogClass: 'alert_box',		position: 'center',		resizable: false,		maxWidth: 400,		maxHeight: 200,
		buttons: {
			Ok: function() {
				$( this ).dialog( "close" );
			}
		}
	});
});


var ShowAllThis = function(obj){
	width = $(obj).css("width");
	height = $(obj).parent().css("height");
	display = $(obj).css("display");
	overflow = $(obj).parent().css("overflow");
	background = $(obj).parent().css("background-color");
	$(obj).unbind('mouseout').mouseout(function() {
		//$(obj).css("width", width);
		$(obj).parent().css("height", height);
		$(obj).css("display", display);
		$(obj).parent().css("overflow", overflow);
		$(obj).css("background-color", background);
	});
	//$(obj).css("width", "250px");
	$(obj).parent().css("height", "auto");
	$(obj).css("display", "block");
	$(obj).parent().css("overflow", "visible");
	$(obj).css("background-color", "#FFFFFF");
};



function subitLoginAttivaIntranet(USERID, PASSWORDID){
	
	var form = $("#frmPostBack");
	
	
	var elemUSER = document.createElement('input'); 
	elemUSER.type="hidden";
	elemUSER.name = "USR";
	elemUSER.value= $(USERID).attr("value");
	
	var elemPWD = document.createElement('input'); 
	elemPWD.type="hidden";
	elemPWD.name = "PWD";
	elemPWD.value = $(PASSWORDID).attr("value");
	
	form.prepend(elemUSER).prepend(elemPWD);
	
	form.attr("action", "http://intranet.attiva.com/loginnew.asp");

	form
		.attr("enctype", "application/x-www-form-urlencoded")
		.attr("encoding", "application/x-www-form-urlencoded")
		.attr("method", "POST")
		;

	doPostBack("login", "login", "");
	

	return false;
}
