// JavaScript Document


var before=[];
var after=[];
var extendResizzle=function(){};
var colw=160;
var hmod=160;
var menuresizable=true;

var resizable=false;

var menumax=false;
var masoblock=false;

var menumin=1;

var mainmin=4;

var maxlayoutcol=8;
var minlayoutcol=6;

var barspace=2;
var extraright=40;

var minwidth=(colw*minlayoutcol)+extraright;

var animatesocial=true;


var ww, num_col, aw, a_l, mw, w100, mainw, main, menu, diff, diff2;
	 
	 var resizzle=function(){
		
		
		ww=$('#wrapper').width()-barspace-extraright;
		//var a_l= ww-(Math.floor(ww/160)*160)/2-1;
		
		num_col=Math.floor(ww/colw);
		
		if(ww<minwidth){
			num_col=6;
			ww=minwidth;
		}
		
		if((maxlayoutcol)&&(num_col>maxlayoutcol)){
			num_col=maxlayoutcol;
			
		}
		
		diff=num_col-1-mainmin-menumin;
		if(diff>0){
			if(menumax){
				diff2=num_col-1-mainmin-menumax;
				if(diff2<=0){
					menu=menumin+diff;
					main=mainmin;
				}
				else{
					menu=menumax;
					main=mainmin+diff2;
				}
			}
			else{
				menu=menumin+diff;
				main=mainmin;
			}
		}
		else{
			menu=menumin;
			main=mainmin
		}

		mw=menu*colw;
		
		mainw=main*colw;

		
		
		
		aw=num_col*colw;

		a_r=(ww-aw)/2-1;
		a_l=a_r+1;
		//a_r+=extraright;
		aw=(num_col-2)*colw;
		w100=aw-20;
		
		//alert((aw+a_l+a_r+241+120)+'-'+($('#wrapper').width()));
		/*$('#grid').html('');
		for (i=0; i<(num_col-2); i++) 
			{$('#grid').append('<div class="box col1 "> &nbsp;</div>');}
		
		if(sliderresizable)
		{
			$('.resizable').css("width",(w100-colw)+'px');
			$('#slider').css("width",(w100)+'px');
		}
		*/
		
				

		
		/*$('.rszbl').css("marginLeft",(a_l)+'px');
		$('.rszbl').css("marginRight",(a_l)+'px');
		$('.rszbl').css("width",(aw)+'px');
		*/
		$('#scroller').css("marginLeft",(a_l+241)+'px');
		$('#scroller').css("marginRight",(a_l+120)+'px');
		$('#scroller').css("width",(aw)+'px');
		$('#innerscreen').css("marginLeft",(a_l+241)+'px');
		$('#innerscreen').css("marginRight",(a_l+120)+'px');
		$('#innerscreen').css("width",(aw)+'px');
		
		$('#socialtab').css("marginLeft",(a_l+241)+'px');
		$('#socialtab').css("marginRight",'10px');
		$('#socialtab').css("width",(aw+110+a_l+extraright)+'px');
		
		$('#grid').css("marginLeft",(a_l)+'px');
		$('#grid').css("marginRight",(a_l)+'px');
		
		$('#grid').css("width",(aw+1)+'px');
		
		setW('.colmenu',mw);
		setW('#menu .colmenu',mw-1);
		
		$('#menu').css("width",(mw-3)+'px');
		
		setW('.col1000',aw-21);
		setW('.col1001',aw+239);
		setW('.col501',(aw+239)/2);
		
		setW('.col100',aw);
		$('#manualpage').css("width",(mainw-colw)+'px');
		$('.logolabel').css("width",(mainw-colw-20)+'px');
		
		$('#main').css("width",(mainw)+'px');
		//setW('#main',mainw);
		if(resizable)
		{
			setW('.resizable',mainw);
			
		}
		else{
			setW('.resizable',mainmin*colw);
		}
		
		extendResizzle();
		
		if($('.masonry-brick').size()>0)
			{
				//$('#menucontainer').masonry('reload')
				if(masoblock){$('.masoblock').each(function() {$(this).masonry('reload');})}
				$('#scroller').masonry('reload');
				
			}
		else
		{
			/*$('#menucontainer').masonry({
				columnWidth: colw, 
				itemSelector: '.boxa:not(.invis)',
				isAnimated:false,
				isResizable:false
			});*/
			if(masoblock){
				$('.masoblock').each(function() {
					$(this).masonry({
						columnWidth: colw, 
						itemSelector: '.boxa:not(.invis)',
						isAnimated:false,
						isResizable:false,
						isRTL:$(this).hasClass('RTL')
						});
				});
				
			}
			$('#scroller').masonry({
				columnWidth: colw, 
				itemSelector: '.box:not(.invis)',
				isAnimated:false,
				isResizable:false
			});
			}
		$('#grid').css("height",$('#scroller').css("height"));
		$('#screen').css("height",$('#scroller').css("height"));
		//$('#menuscreen').css("height",$('#scroller').css("height"));
		
		$("#menuscreen").css('top',$("#menu").css('top'));
		$("#menuscreen").css('left',$("#menu").css('left'));
		
		
   	 };
	
	//////////////////////////
	var setW=function( selector, w){
		
		$(selector).each(function() {
			var m=parseInt($(this).css('marginLeft'),10)
				 +parseInt($(this).css('marginRight'),10)
				 +parseInt($(this).css('paddingLeft'),10)
				 +parseInt($(this).css('paddingRight'),10)
				 +parseInt($(this).css('borderLeftWidth'),10)
				 +parseInt($(this).css('borderRightWidth'),10);
				 
			var colm=Math.floor(m/colw);	 
			$(this).css("width",(w-m-(colm*10))+'px');
		});
	};
	
	//////////////////////////
	
	var gridizzle=function( selector){
		$(selector).each(function() {
    		var h=$(this).height();
			var m=parseInt($(this).css('marginTop'),10)
				 +parseInt($(this).css('marginBottom'),10)
				 +parseInt($(this).css('paddingTop'),10)
				 +parseInt($(this).css('paddingBottom'),10)
				 +parseInt($(this).css('borderTopWidth'),10)
				 +parseInt($(this).css('borderBottomWidth'),10);
		    var nhmod=Math.floor((m+h)/hmod);
			var newh=nhmod*hmod-m;
		    if(newh<h){nhmod++; newh=nhmod*hmod-m;}
			$(this).height(newh);
			
  		});
	};	
	//////////////////////////
	function getH( selector){
		
		var h=$(selector).height();
		var m=parseInt($(selector).css('marginTop'),10)
			 +parseInt($(selector).css('marginBottom'),10)
			 +parseInt($(selector).css('paddingTop'),10)
			 +parseInt($(selector).css('paddingBottom'),10)
			 +parseInt($(selector).css('borderTopWidth'),10)
			 +parseInt($(selector).css('borderBottomWidth'),10);
			 
		h+=m;
		
		return h;
	}
	
	function setH( selector,h){
		
		$(selector).height();
		var m=parseInt($(selector).css('marginTop'),10)
			 +parseInt($(selector).css('marginBottom'),10)
			 +parseInt($(selector).css('paddingTop'),10)
			 +parseInt($(selector).css('paddingBottom'),10)
			 +parseInt($(selector).css('borderTopWidth'),10)
			 +parseInt($(selector).css('borderBottomWidth'),10);
			
		h-=m;
		
		$(selector).height(h);
	}

	
	////////////////////////// MOBILE DETECT
	
	function is_mobile_device() {
		if (typeof window.onorientationchange != "undefined") {
			return true;
		}
		else {
			return false;
		}
	}
	 
	
	 /* iscroll fuck!!!!
	 function loaded() {
		document.addEventListener('touchmove', function(e){ e.preventDefault(); });
		var myScroll = new iScroll('scroller');
	 }
	 if (is_mobile_device()){document.addEventListener('DOMContentLoaded', loaded);} */	 

	 
	 //////////////////////////
	 
	 
	 function setMT(metaName, name, value) {
		
        var t = 'meta['+metaName+'='+name+']';
		
        var mt;// = $(t);
        //if (mt.length === 0) {
            t = '<meta '+metaName+'="'+name+'" />';
            
		   	mt = $(t).appendTo('head');
       // }
        mt.attr('content', value);
    }
	
	//////////////////////////
	 
	 $(function(){		
			
		
		////////////////////////// PAGE FUNCTIONS BEFORE 
		$.each(before, function(index, value) {
			value();	
		});
		
		$(".bodycontent").append("<p class='last'></p>");
		
		////////////////////////// MENU CLONE
		
		//$("#menu").clone().attr("id","menuscreen").appendTo("#innerscreen");
		
		
		
		////////////////////////// RESIZE + SET MARGIN + MASONRY 
		
		var oldhmod=hmod;
		hmod=20;
		gridizzle('#menucontainer > .boxa');
		hmod=oldhmod;
		resizzle();
		
		$(window).bind('resize', resizzle);	
		/*
		$(window).bind('scroll', function() {

			 var st=$('body').scrollTop()-1502+'px';
			 
			 $("#menufix").clearQueue();
			 $('#menufix').animate({top: st}, 200);
			 //$('#menufix').css('top',st);
		});	
		*/
		
		var controlhover;
		
		////////////////////////// MENU ROLLOVER 
		
		
		$("#menu").hover(function(){
								  $(this).addClass('hovered');
								  //$('#menu .submenu li').stop(true,true).slideDown('fast');
								  },
						function(){
								 $('sub_exp').removeClass('sub_exp');
								 $(this).removeClass('hovered');
								 $('#menu .submenu li').stop(true,true).slideUp('fast');
								
								 });
		
		
		
		$(".submenu").prev().bind('click',function(event){
			 event.preventDefault();
			if($(this).hasClass('sub_exp'))
			{
				$(this).removeClass('sub_exp');
				$(this).next().find('li').each(function(){
					$(this).stop(true,true).slideUp('fast');
					
				});
			}
			else {
				$(this).addClass('sub_exp');
				$(this).next().find('li').each(function(){
					$(this).stop(true,true).slideDown('fast');
					
				});	
			}
			
		}
		
		);
		
		/*
		$("#menuscreen").hover(function(){$(this).addClass('hovered');},function(){$(this).removeClass('hovered');});
		
		
		$("#menu").bind('mouseenter',function() {
											   //$('#menuscreen').css('display','block');
											   
											  clearInterval(controlhover);
											   var controlhover = setInterval(function(){ //alert($('#screen').hasClass('hovered'));
																					   if( ($('#menuscreen').hasClass('hovered'))||($('#menu').hasClass('hovered'))){ 
																						   $('#screen').stop(true,true).slideDown('fast');
																						   $('#menuscreen .submenu li').stop(true,true).slideDown('fast');
																							clearInterval(controlhover);
																						}
																						},100);
											  
											  
											   
											  
											  });
		
		$("#menuscreen").bind('mouseleave',function() {
													 
													 $('#screen').stop(true,true).slideUp('fast');
													
													 $('#menuscreen .submenu li').stop(true,true).slideUp('fast');
													});
	 
	    $("#menuscreen").bind('click',function() { $("#menuscreen").trigger('mouseleave') });
		
		$("#menufix").bind('mouseenter',function() {
											  //$('#screen').fadeIn('slow');
											  $("#menufix").addClass('open');
											  $("#menufix").clearQueue();
											  $('#menufix').animate({marginTop: '33px'},300,'easeOutElastic')
											  
											  });
		$("#menufix").bind('mouseleave',function() {
											  $("#menufix").clearQueue();
											  $('#menufix').animate({marginTop: '0px'},300,'easeOutElastic',function() {$("#menufix").removeClass('open');})
											  //$('#screen').fadeOut('fast');
											  });
		$("#menufix").bind('click',function() {
											  $("#menufix").clearQueue();
											  $('#menufix').animate({marginTop: '482px'},300,'easeOutElastic')
											  
											  });*/
		
		////////////////////////// PAGE FUNCTIONS BEFORE 
		$.each(after, function(index, value) {
			value();	
		});
		////////////////////////// VIDEO
		$('.video iframe').each(function(){
			$(this).attr('frameborder',"0");
			$(this).attr('wmode',"Opaque");
			var url=$(this).attr('src')+'?wmode=transparent';
			$(this).attr('src',url);
		
		});
		////////////////////////// SOCIALPANEL
		var wst=$(window).scrollTop();
		var socialmouse=function(){
			$("#socialscreen").bind('mouseenter',function() {
										   
											  $('#socialscreen').stop(true,false).animate({top: '-300px'},1500,'easeOutElastic',function(){$("#socialscreen")});
										   
											  });
			$("#socialscreen").bind('mouseleave',function() {	 
									if(!$('#socialscreen').hasClass('open')){
										$('#socialscreen').stop(true,false).delay(300).animate({top: (-$("#socialscreen").height()-300)+'px'},800,'easeOutElastic',function(){$("#socialscreen")});}
									
											  
											  });
			$("#socialscreen").bind('click',function() {$("#socialscreen").trigger('mouseleave').trigger('mouseenter')});
		}
		
		
		$("#socialscreen").focusin(function() {
											
											$('#socialscreen').addClass('open');
											
											
											});
		
		$("#socialscreen").focusout(function() {
											
											$('#socialscreen').removeClass('open');
											
											
											});
		
		var socialtop='-300px';
		
		$("#socialscreen").css('top',(-$("#socialscreen").height()-360)+'px');
		
		$("#socialscreen").show();
		/*if(animatesocial){
		$('#socialscreen').delay(1000).animate({top:(-$("#socialscreen").height()-250)+'px'},100).animate({top: (-$("#socialscreen").height()-300)+'px'},500,'easeOutElastic', socialmouse);
		 
		}
		else{*/
		$('#socialscreen').delay(1000).animate({top:(-$("#socialscreen").height()-300)+'px'},700,'easeOutElastic', socialmouse);	
			
			//}
		
		$(window).scroll(function(){
					var wsto=$(window).scrollTop()-wst;
					wst=$(window).scrollTop();
			if  ($(window).scrollTop() == $(document).height() - $(window).height()){
			   $('#socialscreen').stop().animate({top:(-$("#socialscreen").height()-250)+'px'},100).animate({top: (-$("#socialscreen").height()-300)+'px'},500,'easeOutElastic', socialmouse);
			   			}
						
			else{
				if( ($('#socialscreen').hasClass('down') )&& !($('#socialscreen').hasClass('downanimating')) ){
					
				 $('#socialscreen').addClass('downanimating').stop(true,false).animate({bottom: (-$("#socialscreen").height()-360)+'px'},300,function(){$('#socialscreen').removeClass('down').removeClass('downanimating').css('bottom','auto').css('top',(-$("#socialscreen").height()-300)+'px')});
			   			}
				else
				if( ($(window).scrollTop() == 0)&&(!$ ("#socialscreen").hasClass('.open') )){
					
					 $('#socialscreen').stop(true,true).css('top',(-$("#socialscreen").height()-300)+'px').animate({top:(-$("#socialscreen").height()-250)+'px'},100).animate({top: (-$("#socialscreen").height()-300)+'px'},400,'easeOutElastic');
					 }
				else{
						if(($("#socialscreen").hasClass('.open')&&(wsto>0))){$("#socialscreen").trigger('mouseleave');}
					}
			}
			
				 
				
		});
		$('#screen').fadeOut();
		$('#scroller').css('marginTop',0);
	});
