﻿ $(document).ready(function() {

	$('.alpha span').addClass('lock');
	$('.beta').jScrollPane();

	$('.prof').click(function() {
		$('.code').animate({
			left: '954px'
		});
		
		$('.comm').animate({
			left: '1272px'
		});
		
		$('.alpha span', this).delay(500).queue(function(next){
			$(this).removeClass('lock');
			next();
		});
		
		$('.alpha', this).css('cursor', 'default');
	});
	
	$('.code').click(function() {
		$(this).animate({
			left: '0px'
		});
		
		$('.prof').animate({
			left: '-318px'
		});
			
		$('.comm').animate({
			left: '954px'
		});
		
		$('.alpha span', this).delay(500).queue(function(next){
			$(this).removeClass('lock');
			next();
		});
		
		$('.alpha', this).css('cursor', 'default');
	});
	
	$('.comm').click(function() {
		$(this).animate({
			left: '0px'
		});
		
		$('.prof').animate({
			left: '-636px'
		});
		
		$('.code').animate({
			left: '-318px'
		});
		
		$('.alpha span', this).delay(500).queue(function(next){
			$(this).removeClass('lock');
			next();
		});
		
		$('.alpha', this).css('cursor', 'default');
	});
	
	$('.prof .alpha span').click(function(e) {
		e.stopPropagation();
		$('.code').animate({
			left: '318px'
		});
		
		$('.comm').animate({
			left: '636px'
		});
		
		$(this).addClass('lock');
		$(this).parents('.alpha').css('cursor', 'pointer');
	});
	
	$('.code .alpha span').click(function(e) {
		e.stopPropagation();
		$(this).parents('.charlie').animate({
			left: '318px'
		});
		
		$('.prof').animate({
			left: '0px'
		});
			
		$('.comm').animate({
			left: '636px'
		});
		
		$(this).addClass('lock');
		$(this).parents('.alpha').css('cursor', 'pointer');
	});
	
	$('.comm .alpha span').click(function(e) {
		e.stopPropagation();
		$(this).parents('.charlie').animate({
			left: '636px'
		});
		
		$('.prof').animate({
			left: '0px'
		});
		
		$('.code').animate({
			left: '318px'
		});
		
		$(this).addClass('lock');
		$(this).parents('.alpha').css('cursor', 'pointer');
	});

	$('#media .text p').hide();
	$('#media .text p.:first-child').show();
	
	
	$('#media .items img').click(function() {
        $('#media .text p').hide();
		$('#media .text p:eq(' + $(this).index() + ')').show();
		
        $(this).parent()
            .find('img')
            .removeClass('active');
        
        $(this).addClass('active');
        
        $(this).parents('#media')
        	.children('.video')
        	.html($(this).attr('alt'));
        
    });
    
	$('img').load(function(){  
		$('.slides img').each(function() {
		    var parent_height = $(this).parent().height();   
		    var image_height = $(this).height();  
		    var top_margin = (parent_height - image_height)/2;  
		    $(this).css( 'margin-top' , top_margin);  
		});
	}); 
	
	$('#gallery img').click(function() {

		var id = '#pop';
		var id3 = '#threed';
		var clas = '#pop > img';
		var spec = 'curr';
		var spec2 = '.' + spec;

		$(id).clone()
			.appendTo($('body')).css('display','block').addClass(spec);
			
		$($(this).attr('alt')).clone()
			.appendTo($('.curr #slider'))
			.addClass('currslide');
			
		$('.curr #slider ul').show();
		
		if ($('.curr #slider img').size() > 1) {
			$('.curr #slider').easySlider({
				//continuous: true,
				prevId: 'prev',
				nextId: 'next'
			});
		}

		var maskHeight = $(document).height();
		var maskWidth = $(window).width();

		var winH = $(window).height();
		var winW = $(window).width();

		centerWin(spec2);

		$(spec2).fadeIn(2000);
		uiclosepopup(clas, spec2);
	});

	function uiclosepopup(clas, spec2){
		$(clas).click(function(e) {
			e.preventDefault();
			$(spec2).remove();
		});
	}

	function getScrollXY() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return {X:scrOfX, Y:scrOfY};
	}

	function getWindowSize() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		}
		return{X:myWidth, Y:myHeight}
	}

	function centerWin(spec2){
		var windowDim = getWindowSize();
		var popupHeight = $(spec2).height();
		var popupWidth = $(spec2).width();
		var scroll = getScrollXY();
		$(spec2).css({
			"position": "absolute",
			"top": windowDim.Y/2-popupHeight/2 + scroll.Y-15,
			"left": windowDim.X/2-popupWidth/2 + scroll.X-15
		});
	}
	
	if ($('.prof').length > 0) {
	
     var stop = false;

     setInterval(
		function slideSwitch() {
			if(!stop) {
				var active = $('.prof .alpha div img.active');
				if ( active.length == 0 ) active = $('.prof .alpha div img:last');

				var next =  active.next().length ? active.next()
					: $('.prof .alpha div img:first');

				active.addClass('last-active');
				if(next){
				next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, 1000, function() {
						active.removeClass('active last-active');
					});
				}
			}else{
				if(next){
					next.stop();
				}
			}
		},
        6000);
	}
	
	if ($('.comm').length > 0) {
	
     var stop = false;

     setInterval(
		function slideSwitch() {
			if(!stop) {
				var active = $('.comm .alpha div img.active');
				if ( active.length == 0 ) active = $('.comm .alpha div img:last');

				var next =  active.next().length ? active.next()
					: $('.comm .alpha div img:first');

				active.addClass('last-active');
				if(next){
				next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, 1000, function() {
						active.removeClass('active last-active');
					});
				}
			}else{
				if(next){
					next.stop();
				}
			}
		},
        6000);
	}
	
	if ($('.code').length > 0) {
	
     var stop = false;

     setInterval(
		function slideSwitch() {
			if(!stop) {
				var active = $('.code .alpha div img.active');
				if ( active.length == 0 ) active = $('.code .alpha div img:last');

				var next =  active.next().length ? active.next()
					: $('.code .alpha div img:first');

				active.addClass('last-active');
				if(next){
				next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, 1000, function() {
						active.removeClass('active last-active');
					});
				}
			}else{
				if(next){
					next.stop();
				}
			}
		},
        6000);
	}
    
});
