var items = 0;
var iter = 0;
var colors = ['purple','red','green','blue','yellow'];

$(document).ready(function(){
	$(document).pngFix();
	addEventListeners();

});

$(window).load(function(){
	var hash = window.location.hash;
	if(hash == '#client_login'){
		$(".copyright").css("display","none");
		showLogin();
	}
	else if(hash == '#contact'){
		$(".copyright").css("display","none");
		showContact();
	}

	else if(hash == '#careers'){
		$(".copyright").css("display","none");
		showCareers();
	}

	else {
		loadMainMenu();
		$(".copyright").delay(2000).fadeIn(2000);
	}
});

$(window).resize(function(){updateOrientation();});

function updateOrientation(){
	$('#top').css('width','100%');
	$('#blkbar').css('width','100%');
}

function loadMainMenu(){
	$('#home_main_title').hide();
	$('#home_main_sub').hide();
	$('#home_main_sub_title').hide();
	$('#home_menu_panel').hide();
	$('#main_home_block').hide();
	$('#home_menu').addClass('open');
	$('#home_menu').delay(750).slideDown(600,'easeOutCubic');
	
	$('#home_main_title').delay(1350).show('slide',{direction:'left',easing:'easeOutCubic'},200);
	$('#home_main_title').delay(2000).fadeOut(200);
	
	$('#home_main_title1').delay(4000).fadeIn(200);
	$('#home_main_title1').delay(1500).fadeOut(200);
	
	$('#home_main_title2').delay(6150).fadeIn(200);
	$('#home_main_title2').delay(1500).fadeOut(200);
	
	$('#home_main_title3').delay(8300).fadeIn(200);
	$('#home_main_title3').delay(1500).fadeOut(200);
	
	$('#home_main_title4').delay(10350).fadeIn(200);
	$('#home_main_title4').delay(1500).fadeOut(200);
	
	$('#home_main_title5').delay(13000).fadeIn(10);
	$('#payoff1').delay(13500).fadeIn(200);
	$('#payoff2').delay(15000).fadeIn(200);
	$('#home_main_sub_title').delay(16500).fadeIn(200);
	$('#home_main_sub').delay(18000).fadeIn(200);
	$('#sce').delay(18500).show('slide',{direction:'left',easing:'easeOutCubic'},400);
	$('#home_menu_panel').delay(18700).show('slide',{direction:'left',easing:'easeOutCubic'},400);
}

function thisAnimationSucks() {
	$("#home_menu *").stop(true);
	$('#home_main_title').hide();
	$('#home_main_title1').hide();
	$('#home_main_title2').hide();
	$('#home_main_title3').hide();
	$('#home_main_title4').hide();
	$('#home_main_title5').show();
	$('#payoff1').show();
	$('#payoff2').show();
	$('#home_main_sub_title').show();
	$('#home_main_sub').show();
	$('#sce').show();
	$('#home_menu_panel').show();
}

function closeMainMenu(){$('#main_home_block').animate({paddingTop:'75px'},600,'easeInOutCubic');
	$('#spacer').animate({height:100+$('#main_home_block').height()+'px'},600,'easeInOutCubic');
	$('#home_menu').slideUp(600,'easeInOutCubic',function(){
		$(this).removeClass('open').addClass('closed');
		$(this).hide();
		thisAnimationSucks();
		//loadMainTray();
	});
}

function openMainMenu(){

	if( $('#login').hasClass('active') ){$('#login').removeClass('active');}
	if( $('#contact').hasClass('active') ){$('#contact').removeClass('active');}
	if( $('#careers').hasClass('active') ){$('#careers').removeClass('active');}

	
	$('#main_home_block').animate({paddingTop:'75px'},600,'easeInOutCubic');
	$('#spacer').animate({height:500+$('#main_home_block').height()+'px'},600,'easeInOutCubic');
	$('#home_menu').slideDown(600,'easeInOutCubic',function(){$(this).removeClass('closed').addClass('open');});



	var hash = window.location.hash;

	if(items == '0' || hash == '#contact' || hash == '#careers' || hash == '#client_login') {
		$('#menu_close,#menu_view').css('display','none');	
		$('.mitem').removeClass('active');
		$('.mitem').children('.micon').removeClass('active');
		$('.titem').removeClass('active');
		$('.titem').children('.ticon').removeClass('active');
		$('#contents').html('');
		clearExtras();
		window.location.hash = '';
	}
	
	else {$('#menu_close,#menu_view').css('display','block');}



}

function closeAllMenus(){
	if( $('#home_menu').hasClass('open') ) closeMainMenu();
	else if( $('#home_tray_arrow').hasClass('open') ) closeMainTray();
}

function loadMainTray(){

	//Override for deep links
	$('#main_home_block').css('paddingTop','75px');
	$('#spacer').css('height',100+$('#main_home_block').height()+'px');

	$('#home_tray').css('top','-575px');
	$('#home_tray').delay(50).show().animate({top:'-390px'},600,'easeOutCubic',function(){
		$('#relaunch').fadeIn();
		$('#home_tray_arrow').removeClass('open').addClass('closed');
		$('#home_tray_arrow').click(function(){if($(this).hasClass("closed")){openMainTray();} else {closeMainTray();}
		});
	});
}

function openMainTray(){
	$('#relaunch').fadeOut();
	$('#home_tray_arrow').removeClass("closed").addClass("open");
	$('#home_tray_arrow').fadeOut('fast','swing',function(){
		$('#home_tray_arrow').css('backgroundPosition','0px 0px');
		$('#home_tray_arrow').fadeIn('fast');
	});
	$('#home_tray').animate({top:'-2px'},600,'easeInOutCubic');
}

function closeMainTray(){
	$('#home_tray_arrow').removeClass("open").addClass("closed");
	$('#home_tray_arrow').fadeOut('fast','swing',function(){
		$('#home_tray_arrow').css('backgroundPosition','0px -50px');
		$('#home_tray_arrow').fadeIn('fast');
	});
	$('#home_tray').animate({top:'-390px'},600,'easeInOutCubic',function(){$('#relaunch').fadeIn();});
}

function addEventListeners(){


	$('.mitem').click(function(){

		$(this).css('opacity',1);
		var rel = $(this).attr('rel');
		
		if($(this).children('.micon').hasClass('active')){
			$(this).children('.micon').removeClass('active');
			$('.titem[rel='+rel+']').children('.ticon').removeClass('active');
			dropSection(rel);


			if(items == '1') {$('#menu_close,#menu_view').css('display','none');}

		} else {
			$(this).children('.micon').addClass('active');
			$('.titem[rel='+rel+']').children('.ticon').addClass('active');
			loadSection(rel,'prepend');
		}
	});

	$('.mitem').mouseenter(function(){$(this).animate({opacity:0.5},100,'easeOutCubic');
	});

	$('.mitem').mouseleave(function(){$(this).animate({opacity:1},100,'easeOutCubic');
	});

	$('#menu_go').click(function(){closeMainMenu();});

	$('#menu_clear').click(function(){
		$('.mitem').removeClass('active');
		$('.mitem').children('.micon').removeClass('active');
		$('.titem').removeClass('active');
		$('.titem').children('.ticon').removeClass('active');
		$('#contents').html('');
		$('#menu_close,#menu_view').css('display','none');
		items = 0;

	});

	$('#menu_select').click(function(){

		$('#contents').html('');		
	
		$('.mitem').addClass('active');
		$('.mitem').children('.micon').addClass('active');
		$('.titem').addClass('active');
		$('.titem').children('.ticon').addClass('active');
	

   		 $($('.titem').get().reverse()).each(function(index){
  		        var self = this
       			setTimeout(function () {
				var rel = $(self).attr('rel');    	
			        loadSection(rel,'prepend');
			}, index*500);
   		 });
	
		
	});

	$('#menu_close,#menu_view').click(function(){

		closeMainMenu();
		$('#menu_tongue_holder').delay(600).animate({bottom: -60}, 500, 'easeOutCubic');

	
	});

	$('#menu_tongue_arrow').click(function(){
		openMainMenu();
		$('#menu_tongue_holder').animate({bottom: 60}, 500, 'easeOutCubic');
	});





	$('.titem').click(function(){
		$(this).css('opacity',1);
		var rel = $(this).attr('rel');

		if($(this).children('.ticon').hasClass('active')){
			$(this).children('.ticon').removeClass('active');
			dropSection(rel);
		} else {
			$(this).children('.ticon').addClass('active');
			loadSection(rel,'prepend');
		}
	});

	$('.titem').mouseenter(function(){$(this).animate({opacity:0.5},100,'easeOutCubic');
	});

	$('.titem').mouseleave(function(){$(this).animate({opacity:1},100,'easeOutCubic');
	});

	$('#tray_go').click(function(){closeMainTray();});

	$('#tray_clear').click(function(){
		$('.mitem').removeClass('active');
		$('.mitem').children('.micon').removeClass('active');
		$('.titem').removeClass('active');
		$('.titem').children('.ticon').removeClass('active');
		$('#contents').html('');
	});

	

	
}

function loadSection(path,pos){

	//clearExtras();

	$.ajax({
  	url: 'http://www.group360.com/ee/index.php/section/load/'+path,
  	context: $('#contents'),
  	success: function(data){
		var color = getColor();
		var $sect = $('<div class="sect '+color+'" rel="'+path+'">'+data+'</div>');
		if(pos == 'append') $(this).append($sect);
		else $(this).prepend($sect);
		$($sect).delay(100).slideDown('slow','easeOutCubic');
		$($sect).children('.smenu').delay(1000).show('slide',{direction:'right',easing:'easeOutCubic'},250);
		items++;
	
		$('#menu_close,#menu_view').css('display','block');


		var thisrel = $($sect).find('.thumb_link').attr('rel');
				

		$("a[rel="+thisrel+"]").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
			    return '<span id="fancybox-title-over">' + title + '</span>';
			}
		});

		$('.thumb_link').hover(
 		 function () {
    			$(this).find('.graythumb').css("display","none");
			$(this).find('.colorthumb').css("display","inline");
  		}, 
  		function () {
    			$(this).find('.graythumb').css("display","inline");
			$(this).find('.colorthumb').css("display","none");

  		}
	);


 	}});
}

function loadExtra(path){

	$.ajax({
  	url: 'http://www.group360.com/ee/index.php/section/load/'+path,
  	context: $('#extras'),
  	success: function(data){

		$(this).append(data);
		$(this).slideDown('slow','easeOutCubic');
 	}});
}

function clearExtras(){
	if( $('#contents').hasClass('closed') ){
		window.location.hash = '';
		$('#login').removeClass('active');
		$('#contact').removeClass('active');
		$('#careers').removeClass('active');
		$('#extras').html('');
		/*$('#extras').removeClass('open').addClass('closed').slideUp('slow','easeOutCubic',function(){$('#contents').removeClass('closed').addClass('open').slideDown('slow','easeOutCubic');});*/
		$('#extras').removeClass('open').addClass('closed');
		$('#contents').removeClass('closed').addClass('open').slideDown('slow','easeOutCubic');
	}
	
	$('#extras').html('');

}

function toggleSection(path){
	var copy = $('.sect[rel='+path+']').children('.scopy');
	var cats = $('.sect[rel='+path+']').children('.stypes');
	var arrow = $('.sect[rel='+path+']').children('.stitle').children('.stitle_arrow');
	
	if( $(copy).hasClass('closed') ){
		$(arrow).removeClass('closed');
		$(copy).removeClass('closed').slideDown('fast','easeOutCubic');
		$(cats).fadeIn('fast','easeInOutCubic');
		
	} else {
		$(arrow).addClass('closed');
		$(copy).addClass('closed').slideUp('fast','easeOutCubic');
		$(cats).fadeOut('fast','easeInOutCubic');
	}
}

function dropSection(path){

	clearExtras();
	if(path == 'site_careers') $('#careers').removeClass('active');

	$('.sect[rel='+path+']').slideUp('fast','easeOutCubic',function(){
		$(this).remove();
		$('.mitem[rel='+path+']').children('.micon').removeClass('active');
		$('.titem[rel='+path+']').children('.ticon').removeClass('active');
		
		items--;
		if(items == 0 && $('#home_menu').hasClass('closed')) {
			openMainMenu(); 
			$('#menu_tongue_holder').animate({bottom: 30}, 500, 'easeOutCubic');
			$('#menu_close,#menu_view').css('display','none');	
		}

		if(items == 0) {
			$('#menu_tongue_holder').animate({bottom: 30}, 500, 'easeOutCubic');
			$('#menu_close,#menu_view').css('display','none');		
		}
		



	});
}

function getColor(){
	var color = colors[iter];
	iter++;
	if(iter >= colors.length) iter = 0;
	return color;
}

function showLogin(){

	$(".copyright").css("display","none");

	if( $('#careers').hasClass('active') ){hideCareers();}

	if( $('#contact').hasClass('active') ){hideContact();}

	closeAllMenus();
	closeMainMenu();

	window.location.hash = 'client_login';
	$('#login').addClass('active');

	
	$('#contents').html('');
	$('#menu_tongue_holder').delay(600).animate({bottom: -60}, 500, 'easeOutCubic');


	$('#contents').removeClass('open').addClass('closed').slideUp('slow','easeOutCubic',function(){
		$('#extras').removeClass('closed').addClass('open').slideUp('slow','easeOutCubic',function(){
			$(this).html('');
			loadExtra('login');
			$(".copyright").delay(2000).fadeIn(2000);
		});
	});
}

function hideLogin(){
	window.location.hash = '';
	$('#login').removeClass('active');
	$('#extras').removeClass('open').addClass('closed').slideUp('slow','easeOutCubic',function(){$('#contents').removeClass('closed').addClass('open').delay(250).slideDown('slow','easeOutCubic');});
}

function showContact(){

	$(".copyright").css("display","none");

	if( $('#login').hasClass('active') ){hideLogin();}

	if( $('#careers').hasClass('active') ){hideCareers();}

	closeAllMenus();
	closeMainMenu();

	window.location.hash = 'contact';
	$('html, body').animate({ scrollTop: 0 }, 0);
	$('#contact').addClass('active');

	
	$('#contents').html('');
	$('#menu_tongue_holder').delay(600).animate({bottom: -60}, 500, 'easeOutCubic');


	$('#contents').removeClass('open').addClass('closed').slideUp('slow','easeOutCubic',function(){
		$('#extras').removeClass('closed').addClass('open').slideUp('slow','easeOutCubic',function(){
			$(this).html('');
			loadExtra('contact');
			$(".copyright").delay(2000).fadeIn(2000);
		});
	});


}

function hideContact(){
	window.location.hash = '';
	$('#contact').removeClass('active');
	$('#extras').removeClass('open').addClass('closed').slideUp('slow','easeOutCubic',function(){$('#contents').removeClass('closed').addClass('open').delay(250).slideDown('slow','easeOutCubic');});
}

function showCareers() {

	$(".copyright").css("display","none");

	if( $('#login').hasClass('active') ){hideLogin();}

	if( $('#contact').hasClass('active') ){hideContact();}



	closeAllMenus();
	closeMainMenu();

	window.location.hash = 'careers';
	$('html, body').animate({ scrollTop: 0 }, 0);
	$('#careers').addClass('active');

	
	$('#contents').html('');
	$('#menu_tongue_holder').delay(600).animate({bottom: -60}, 500, 'easeOutCubic');


	$('#contents').removeClass('open').addClass('closed').slideUp('slow','easeOutCubic',function(){
		$('#extras').removeClass('closed').addClass('open').slideUp('slow','easeOutCubic',function(){
			$(this).html('');
			loadExtra('site_careers');
			$(".copyright").delay(2000).fadeIn(2000);
		});
	});

}

function hideCareers(){
	window.location.hash = '';
	$('#careers').removeClass('active');
	$('#extras').removeClass('open').addClass('closed').slideUp('slow','easeOutCubic',function(){$('#contents').removeClass('closed').addClass('open').delay(250).slideDown('slow','easeOutCubic');});
}

function viewJob(id){
	var win = null;
	var w = 600;
	var h = screen.height - 200;
	var leftPos = (screen.width) ? (screen.width-w)/2 : 0;
	var topPos = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+topPos+',left='+leftPos+',scrollbars=yes,resizable'
	win = window.open('http://www.group360.com/ee/index.php/careers/'+id,'careers',settings);
}
<!-- Localized -->
