var ie4=document.all;
var ns6=document.getElementById&&!document.all;

timer = 5000;
objDropDownItem = new Object;
objDropDownItem="";
intOpacity = 100;


$(document).ready(function(){
	var language = $('form').attr('lang')?$('form').attr('lang'):'en';
	manageContactsForm(language);

        $(".subPopup").mouseenter(function()
        {            
            $(this).find('ul').css("display", "block");
        })
        $(".subPopup").mouseleave(function()
        {
            $(this).find('ul').css("display", "none");
        })
});

function manageContactsForm(language){

	var fullNameString, firstNameString, lastNameString;

	switch(language){
		case 'it': {
			fullNameString = 'Dati personali';
			firstNameString = 'Nome';
			lastNameString = 'Cognome';
		}; break;
		default: {
			fullNameString = 'Full name';
			firstNameString = 'First';
			lastNameString = 'Last';
		}; break;
	}

	var firstNameField = $('input#contacts_first');
	var firstNameLabel = $(firstNameField).siblings('label');
	var lastNameField = $('input#contacts_last');
	var lastNameLabel = $(lastNameField).siblings('label');

	$(firstNameLabel).html(fullNameString);
	$(firstNameField).addClass('half-sized');
	if($(firstNameField).val()==""){
		$(firstNameField).attr('value',firstNameString);
		$(firstNameField).addClass('inactive');
	}

	if((firstNameLabel).siblings().is('ul.error_list')){
		$(firstNameLabel).siblings('ul.error_list').addClass('half-sized');
	}

	var lastNameFieldClone = $(lastNameField).clone();
	$(lastNameFieldClone).addClass('half-sized');
	$(lastNameFieldClone).addClass('last');
	if($(lastNameField).val()==""){
		$(lastNameFieldClone).attr('value',lastNameString);
		$(lastNameFieldClone).addClass('inactive');
	}
	else{
		$(lastNameFieldClone).attr('value',$(lastNameField).val());
	}

	$(lastNameFieldClone).insertAfter(firstNameField);

	if($(lastNameField).parent('li').children().is('ul')){
		var lastNameError = $(lastNameField).siblings('ul.error_list').find('li');
		var lastNameErrorClone = $(lastNameError).clone();
		var firstNameGroup = $(lastNameError).parent('ul').parent('li').parent('ul').children('li').slice(0,1);
		$(firstNameGroup).children('ul.error_list').addClass('half-sized');
		if($(firstNameGroup).children().slice(0,1).is('ul')){
			$(lastNameErrorClone).addClass('error-lastname');
			$(firstNameGroup).children('ul.error_list').append(lastNameErrorClone);
		}
		else{
			var errorList = document.createElement('ul');
			$(lastNameErrorClone).addClass('error-lastname-single');
			$(errorList).addClass('error_list');
			$(errorList).addClass('half-sized');
			$(errorList).append(lastNameErrorClone);
			$(errorList).insertBefore($(firstNameGroup).first());
		}
	}

	$(lastNameLabel).parent('li').detach();
	$(lastNameError).parent('ul').remove();

	$(firstNameField).focus(function(){
		if($(this).val()==firstNameString){
			$(this).val('');
			$(this).removeClass('inactive');
		}
	});

	$(lastNameFieldClone).focus(function(){
		if($(this).val()==lastNameString){
			$(this).val('');
			$(this).removeClass('inactive');
		}
	});

	$('ul.error_list li').css('float','left');
	$('ul.error_list').append('<div class="clear" />');

	/* CONTACS FORM ICONS */

	var emailField = $('input#contacts_email');

	$(emailField).css({'padding-left':'32px', 'width':'340px'});

	var emailIcon = document.createElement('div');
	$(emailIcon).addClass('contacts-email');
	$(emailIcon).addClass('contacts-icon');
	$(emailIcon).insertBefore(emailField);

	$(emailField).focus(function(){
		$(emailIcon).addClass('focused');
	});
	$(emailField).blur(function(){
		$(emailIcon).removeClass('focused');
	});

	$('form').submit(function(){
		$('input.inactive').val('');
	});
}

$(document).ready(function(){
	homeAnimation();
});

function homeAnimation(){
	var cycle_timeout = 5000;
	var cycle_speed = 1250;
	
	var container = $('div.splashes');
	$(window).load(function(){
		
		$(container).find('img').each(function(){
			$(this).show();
		});
		
		var corners = $('div.corners');
		$(corners).append('<div class="left-corner" />');
		$(corners).append('<div class="float-left" />');
		$(corners).append('<div class="right-corner" />');
		$(corners).append('<div class="clear" />');
		
		$(container).css('background','url(images/splash-slogan/loader.gif) center no-repeat');
		$(container).children(':first-child').fadeIn(cycle_speed, function(){
			$(container).cycle({
				fx: 'scrollLeft',
		    speed:  cycle_speed, 
		    timeout: cycle_timeout, 
		    pager:  '.navigator'
			});
		});
		$('div.navigator').find('a').each(function(){
			$(this).wrapInner('<span class="invisible" />');
		});
	});
}


function fnSwitchMenu(objSwitch, strMode, blnTimeout)
{
  if ((objDropDownItem!="")&&(objDropDownItem!=objSwitch))
    fnRemoveMenu();	
  
  fnFindBrowserStyle(document.getElementById(objSwitch)).display=strMode;
  if (objDropDownItem) changeOpacity(100, objDropDownItem);
  intOpacity = 100;
  
  if (blnTimeout)
    objDropDownItem="";
  else
    clearTimeout(timer);
  
  objDropDownItem = objSwitch;  
}

var DDSPEED = 3;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById('ddheader'+id);
  if (document.getElementById('ddcontent'+id))
  {
      var c = document.getElementById('ddcontent'+id);
      c.style.display = "block";
      clearInterval(c.timer);
      if(d == 1){
        clearTimeout(h.timer);
        if(c.maxh && c.maxh <= c.offsetHeight){ return}
        else if(!c.maxh){
          c.style.display = 'block';
          c.style.height = 'auto';
          c.maxh = c.offsetHeight;
          c.style.height = '0px';
        }
        c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
      }else{
        h.timer = setTimeout(function(){ddCollapse(c)},50);
      }
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById('ddheader'+id);
  var c = document.getElementById('ddcontent'+id);
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;	
  }
  c.style.height = currh + (dist * d) + 'px';
  if (!ie4)
  {
	c.style.opacity = currh / c.maxh;
	c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  }
  
  //alert(currh);
  
  if((currh <= 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    if (currh==2) c.style.display = "none";
    clearInterval(c.timer);
  }
}

function fadeMenu()
{
  
  intOpacity-=10;
  
  changeOpacity(intOpacity, objDropDownItem);
  
  if (intOpacity <= 0)
    fnRemoveMenu();    
  else
    timer=setTimeout('fadeMenu()', 50);
}

function fnRemoveMenu()
{
  fnFindBrowserStyle(document.getElementById(objDropDownItem)).display="none";
  changeOpacity(100, objDropDownItem);
  intOpacity = 100;
}



function changeOpacity(newOpacity, objObject)
{
    if (ie4)
        fnFindBrowserStyle(document.getElementById(objObject)).filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+newOpacity+")"
    else
    {
        fnFindBrowserStyle(document.getElementById(objObject)).KhtmlOpacity = (newOpacity/100);
    	fnFindBrowserStyle(document.getElementById(objObject)).MozOpacity = (newOpacity/100);
    }  
}

function highlightMenu(menuId)
{  document.getElementById(menuId).className="menOver";	 }

function unHighlightMenu(menuId)
{  document.getElementById(menuId).className="";  }

function fnFindBrowserStyle(objIdent)
{ return ((ie4||ns6) ? objIdent.style : objIdent); }
