/* !slider */

var slider = {

	elements 	: {},
	vars		: {
		marginLeft	: 30,
		width		: 776
	},

	init: function()
	{
	
		if(document.getElementById("slider"))
		{
			this.elements.images	= $("#slider .slider-image");
			
			if(this.elements.images.length)
			{
			
				// vars
			
				this.vars.stopAnimation		= false;
				this.vars.stopBoxAnimation	= false;
				this.vars.loop				= new utils.loop({min: 0, max: this.elements.images.length - 1});
				this.vars.totalWidth		= ( this.elements.images.length * $(this.elements.images).width() ) + (this.vars.marginLeft * this.elements.images.length);
				
				slider.updateRoundText(this.vars.loop.index);
				slider.fadeOthers(this.vars.loop.index);
				
				$("#slider-bar").css("width", this.vars.totalWidth);
				
				// slider box event
				
				$(this.elements.images)
					.mouseover(function(){
						slider.animateBox(this, 'in');
					})
					.mouseout(function(){
						slider.animateBox(this, 'out');
					});
					
				$("#slider").mouseover(function(){
					clearTimeout(slider.vars.timeout);
				});
				
				$("#slider").mouseout(function(){
					clearTimeout(slider.vars.timeout);
					slider.setTimeout();
				});
				
				// button events
				
				$("#slider-arrow-left").click(function(){
					clearTimeout(slider.vars.timeout);
					slider.animate(0);
				});
				
				$("#slider-arrow-right").click(function(){
					clearTimeout(slider.vars.timeout);
					slider.animate(1);
				});
				
				slider.setTimeout();
				
			}
			
		}
	
	},
	
	animateBox: function(element, direction)
	{
		var currentElement = slider.elements.images[slider.vars.loop.index];
		element = (element === null)? currentElement : element;
		if( currentElement === element )
		{
			$(element).find(".slider-box").stop().animate({
				"left" : (direction === 'in')? 0 : -230
			}, {
				"easing"	: "easeInOutExpo",
				"duration"	: 700,
				"queue"		: false
			});
		}
		
	},
	
	animate: function(direction)
	{
		if(!this.vars.stopAnimation)
		{
			
			this.vars.stopAnimation 	= true;
			this.vars.loop.direction	= direction;
			var currentIndex 			= this.vars.loop.index;
			var nextIndex				= this.vars.loop.next(false);
			var left					= nextIndex * (this.vars.width + this.vars.marginLeft) * -1;
			
			slider.updateRoundText(nextIndex);
			slider.fadeOthers(nextIndex);
			
			$("#slider-bar").animate({
				"left" : left
			}, {
				easing		: "easeInOutExpo",
				duration	: 700,
				complete	: function(){
					slider.vars.stopAnimation = false;
					slider.vars.loop.next();
					slider.setTimeout();
				}
			});
			
		}
	},
	
	setTimeout : function()
	{
		this.vars.timeout = setTimeout(function(){
			slider.animate(1);
		}, 7500);
	},
	
	updateRoundText : function(index)
	{
		$("#slider-round span").text( (index + 1) + "/" + slider.elements.images.length );
	},
	
	fadeOthers : function(index)
	{
		$(slider.elements.images).each(function(indexLoop){
			if(index != indexLoop)
			{
				$(this).animate({
					"opacity"	: .5
				}, {
					"duration"	: 500,
					"easing"	: "easeInOutExpo"
				});
			}
			else
			{
				$(this).animate({
					"opacity"	: 1
				}, {
					"duration"	: 500,
					"easing"	: "easeInOutExpo"
				});
			}
		})
	}

}

/* !home-round */

var homeRound = {

	vars		: {},
	elements	: {},
	init : function()
	{
		if(document.getElementById("home-sections"))
		{
		
			this.elements.spans = $(".home-section span");
			
			$(this.elements.spans).css({"display" : "block", "opacity" : 0, "visibility" : "visible"});
		
			$(this.elements.spans).mouseover(function(){
				homeRound.animate(this, 1);
			});
			$(this.elements.spans).mouseout(function(){
				homeRound.animate(this, 0);
			});
			
			$(".home-section-description p span").css({
				'opacity'		: 0,
				'visibility'	: 'visible'
			});
			
		}
	},
	
	animate : function(element, opacity)
	{
		var index 		= parseInt($(element).parent()[0].id.replace(/home-section-/, ''));
		
		$("#home-section-description-"+index).stop().animate({
			"opacity"	: opacity
		}, {
			"duration"	: duration,
			"easing"	: "easeInOutExpo",
			"queue"		: false
		});
		
		var duration 	= (opacity === 1)? 300 : 500;
		$(element).stop().animate({
			"opacity"	: opacity
		}, {
			"duration"	: duration,
			"easing"	: "easeInOutExpo",
			"queue"		: false
		});
	}

}

/* !link-round */

var linkRound  = {

	elements : {},

	init : function()
	{
		this.elements.links = $(".link-round, .link-round-2");
		$(this.elements.links)
			.css({"background-position": "0 0"})
			.mouseover(function(){
				linkRound.animate(this, "0 -32");
			})
			.mouseout(function(){
				linkRound.animate(this, "0 0");
			});
			
		$(".round-link").hover(
    		function () {
        		$(this).animate({ backgroundColor: "#DA0065" });
    		},
    		function () {
        		$(this).animate({ backgroundColor: "#000000" });
   			}
		);
		
		$(".portfolio-category").hover(
    		function () {
        		$(this).animate({ backgroundColor: "#DA0065" });
    		},
    		function () {
        		$(this).animate({ backgroundColor: "#cccccc" });
   			}
		);
		
		$("#top-logo a").hover(
			function(){
				$(this).fadeTo('fast', 0.5);
			},
			function(){
				$(this).fadeTo('fast', 1);
			}
		);
	},
	
	animate : function(element, value)
	{
		$(element).stop().animate({
			"background-position" : value
		},{
			"duration"	: 200,
			"easing"	: "easeOutExpo",
			"queue"		: false
		});
	}

}

/* !portfolio_slider */

var portfolio_slider = {

	elements	: {},
	vars		: {
	},
	
	init		: function()
	{
		if(document.getElementById("portfolio"))
		{
			portfolio_slider.build(0);
		}
	},
	
	buildSlider : function(dataIndex)
	{
		var portfolioSlider, arrowLeft, arrowRight, portfolioSliderBar;
		
		// portfolio slider
		
		portfolioSlider = document.createElement('div');
		portfolioSlider.setAttribute('id', 'portfolio-slider');
		
		// arrows
		
		arrowLeft		= document.createElement('a');
		arrowLeft.setAttribute('id', 'portfolio-slider-arrow-left');
		
		arrowRight		= document.createElement('a');
		arrowRight.setAttribute('id', 'portfolio-slider-arrow-right');
		
		// portfolio slider bar
		
		portfolioSliderBar	= document.createElement('div');
		portfolioSliderBar.setAttribute('id', 'portfolio-slider-bar');
		
		// append
		
		portfolioSlider.appendChild(arrowLeft);
		portfolioSlider.appendChild(arrowRight);
		portfolioSlider.appendChild(portfolioSliderBar);
		
		// append images
		
		for(var i = 0; i < portfolio_data[dataIndex].images.length; i++)
		{
			var sliderImageDiv, sliderImage, sliderPreload, sliderImageBox, sliderImageBoxH2, sliderImageBoxH4, sliderImageBoxP;
			
			// portfolio-slider-image
			
			sliderImageDiv	= document.createElement('div');
			sliderImageDiv.setAttribute('class', 'portfolio-slider-image');
			
			// portfolio-slider-box
			
			sliderImageBox	= document.createElement('div');
			sliderImageBox.setAttribute('class', 'portfolio-slider-box');
			
			$(sliderImageBox).append('<div class="portfolio-slider-box-content"><a href="'+ portfolio_data[dataIndex].images[i].firstimage +'" class="round-link-2" rel="'+ portfolio_data[dataIndex].images[i].type +'" title="'+ portfolio_data[dataIndex].images[i].firsttext +'"><span>view<br />details</span></a><div class="portfolio-slider-box-title">'+ portfolio_data[dataIndex].images[i].title +'</div><h4>'+ portfolio_data[dataIndex].images[i].customer +'</h4><p>'+ portfolio_data[dataIndex].images[i].description +'</p></div><div class="hidden">'+ portfolio_data[dataIndex].images[i].gallery +'</div>');

			//portfolio-slider-preload
			
			sliderPreload	= document.createElement('div');
			sliderPreload.setAttribute('class', 'portfolio-slider-preload');
			
			// portfolio-slider-img
			
			sliderImage		= document.createElement('img');
			sliderImage.setAttribute('class', 'preload');
			sliderImage.setAttribute('src', portfolio_data[dataIndex].images[i].href);			
			
			sliderImageDiv.appendChild(sliderImageBox);
			sliderImageDiv.appendChild(sliderPreload);
			sliderImageDiv.appendChild(sliderImage);
			
			// append
			
			portfolioSliderBar.appendChild(sliderImageDiv);
		}
		
		return portfolioSlider;
	},
	
	build		: function(index)
	{
		if(portfolio_data.constructor === Array)
		{
			
			this.vars.portfolioIndex = index;
			
			for(var i = 0; i < portfolio_data.length; i++)
			{	
				var strTitle = (i === index)? '<h2 class="portfolio-title-current"><a href="javascript:void(0)">'+ portfolio_data[i].title +'</a></h2>' : '<h2><a href="javascript:void(0)">'+ portfolio_data[i].title +'</a></h2>';
				$("#portfolio").append(strTitle);
				if(i === index)
				{
					$("#portfolio").append(portfolio_slider.buildSlider(0));
				}
			}
			
			// click
			
			$("#portfolio h2 a").click(function(){
				
				var index	= null;
				var links	= $("#portfolio h2 a");
				for(var i = 0; i < links.length; i++)
				{
					if(this === links[i])
					{
						index = i;
					}
				}
				
				if(index !== portfolio_slider.vars.portfolioIndex)
				{
					clearTimeout(portfolio_slider.vars.timeout);
					portfolio_slider.switchSlider(index);
				}
			});
			
		};
		
		portfolio_slider.setup();
		
	},
	
	setup : function()
	{
	
		if(document.getElementById("portfolio"))
		{
			// elements
			
			this.elements.portfolio			= $("#portfolio-slider")[0];
			this.elements.portfolioBar		= $("#portfolio-slider-bar")[0];
			this.elements.images			= $(".portfolio-slider-image");
			
			
			if(this.elements.portfolio && this.elements.images.length)
			{
			
				// vars
			
				this.vars.paddingRight		= parseInt($(this.elements.images[0]).css("padding-right")) * 2;
				this.vars.width 			= $(this.elements.images[0]).width() + 2;
				this.vars.totWidth			= this.vars.width * this.elements.images.length + this.vars.paddingRight * this.elements.images.length;
				this.vars.stopAnimation		= false;
				this.vars.history			= new utils.log();
				
				// events
				
				$("#portfolio-slider-arrow-left").click(function(){
					clearTimeout(portfolio_slider.vars.timeout);
					portfolio_slider.vars.history.log('click-arrow-left');
					portfolio_slider.animate(0);
				});
				
				$("#portfolio-slider-arrow-right").click(function(){
					clearTimeout(portfolio_slider.vars.timeout);
					portfolio_slider.vars.history.log('click-arrow-right');
					portfolio_slider.animate(1);
				});
				
				$(".portfolio-slider-image").mouseover(function(){
					portfolio_slider.vars.history.log('over-image');
					portfolio_slider.animateBox(this, 'in');
				});
				
				$(".portfolio-slider-image").mouseout(function(){
					portfolio_slider.vars.history.log('out-image');
					portfolio_slider.animateBox(this, 'out');
				});
				
				// loop
				
				this.vars.loop				= new utils.loop({min: 0, max: this.elements.images.length - 1});
				
				// set bar width
				
				$(this.elements.portfolioBar).width(this.vars.totWidth);
				
				// deopacize
				
				this.deopacize();
				
				// animate 
				
				this.animate(1);
				
			}
		}
	},
	
	animateBox : function(el, animation)
	{
		if((el === this.elements.images[this.vars.loop.index]) && (!this.vars.stopAnimation) )
		{
			$(el).find('.portfolio-slider-box').stop().animate({
				'bottom' 	: (animation === 'in')? 12 : -145
			}, {
				"duration"	: 400,
				"easing"	: "easeOutExpo",
				"queue"		: false
			});
		}
	},
	
	switchSlider : function(index)
	{
	
		$("h2.portfolio-title-current").removeClass('portfolio-title-current');
		$("#portfolio h2:eq("+ index +")").addClass('portfolio-title-current');
		
		$("#portfolio-slider").css({
			"overflow" : "hidden"
		});
		
		$("#portfolio-slider").animate({
			"height" : 0
		}, {
			"duration"	: 300,
			"easing"	: "easeInOutExpo",
			"queue"		: false,
			"complete"	: function()
			{
			
				portfolio_slider.vars.portfolioIndex = index;
			
				$("#portfolio-slider").remove();
				$("#portfolio h2:eq("+ index +")").after(portfolio_slider.buildSlider(index));
				
				portfolio_slider.setup();
				
				$("#portfolio-slider").css({
					"overflow"	: "hidden",
					"height"	: 0
				});
				
				$("#portfolio-slider").animate({
					"height"	: 360
				}, {
					"duration"	: 300,
					"easing"	: "easeInOutExpo",
					"queue"		: false,
					"complete"	: function()
					{
						$(this).css("overflow", "visible");
					}
				});
				
				preload.init();
				Shadowbox.setup()
			}
		});
		
	},
	
	deopacize	: function(index)
	{
		var opacity;
		$(this.elements.images).each(function(){
			opacity		= (this === portfolio_slider.elements.images[index])? 1 : .2;
			borderColor = (opacity === .2)? '#FFFFFF' : '#CCCCCC';
			
			$(this).css({
				'border-color'	: borderColor
			});
			
			$(this).animate({
				'opacity'		: opacity
			}, {
				"duration"	: 300,
				"easing"	: "easeOutExpo",
				"queue"		: false
			});
		});
		
		
	},
	
	animate		: function(direction)
	{
		if(!this.vars.stopAnimation)
		{
			this.vars.stopAnimation 	= true;
			this.vars.loop.direction	= direction;
			var currentIndex 			= this.vars.loop.index;
			var nextIndex				= this.vars.loop.next(false);
			var left					= nextIndex * (this.vars.width + this.vars.paddingRight) * -1;
			
			portfolio_slider.deopacize(nextIndex);
			
			$('.portfolio-slider-image:eq('+ currentIndex +')').find('.portfolio-slider-box').stop().animate({
				'bottom' 	: -145
			}, {
				"duration"	: 400,
				"easing"	: "easeOutExpo",
				"queue"		: false
			});
			
			$("#portfolio-slider-bar").animate({
				"left" : left
			}, {
				easing		: "easeInOutExpo",
				duration	: 900,
				complete	: function(){
					portfolio_slider.vars.stopAnimation = false;
					portfolio_slider.vars.loop.next();
					portfolio_slider.setTimeout();
				}
			});
			
		}
	},
	
	setTimeout : function()
	{
		this.vars.timeout = setTimeout(function(){
			portfolio_slider.animate(1);
		}, 7500);
	},

}

/* !preload */

var preload = {

	init : function()
	{
		$(".preload").luglio7ImagePreload({
			duration : 1000,
			fadeInit : function(el)
			{
				$(el).prev().fadeOut(300);
			}
		});
	}

}

/* !imageOverflow */

var imageOverlay = {

	init : function()
	{
		$(".image-overlay").css({
			opacity : 0
		});
		$(".image-overlay").parent().mouseover(function(){
			$(this).find('.image-overlay').stop().animate({
				"opacity"	: 1
			}, {
				"duration"	: 1500,
				"queue"		: false,
				"easing"	: "easeOutExpo"
			});
		});
		$(".image-overlay").parent().mouseout(function(){
			$(this).find('.image-overlay').stop().animate({
				"opacity"	: 0
			}, {
				"duration"	: 200,
				"queue"		: false,
				"easing"	: "easeOutExpo"
			});
		});
	}

}

/* !checkbox */

var checkbox = {
	
	elements	: {},
	vars		: {},
	
	init : function()
	{
		
		// vars
		
		$("span.checkbox").click(function(){
			
			var classAttr = $(this).attr("class");
			if($(this).hasClass('checkbox_sel'))
			{
				$(this).removeClass('checkbox_sel');
			}
			else
			{
				$(this).addClass('checkbox_sel');
			}
			
			checkbox.buildData();
			
		});
		
		$(".form li label").click(function(){
			
			var classAttr = $(this).prev().attr("class");
			if($(this).prev().hasClass('checkbox_sel'))
			{
				$(this).prev().removeClass('checkbox_sel');
			}
			else
			{
				$(this).prev().addClass('checkbox_sel');
			}
			
			checkbox.buildData();
			
		});
	},
	
	buildData: function()
	{
		checkbox.vars.data = [];
		var strValue = "";
		$("span.checkbox").each(function(){
			if($(this).hasClass('checkbox_sel'))
			{
				var value = $(this).attr("rel");
				strValue += value + ",";
				checkbox.vars.data.push(value);
			}
		});
		
		strValue = strValue.substr(0, strValue.length - 1);
		$("#checkbox-hidden")[0].value = strValue;
		console.log(strValue);
	}

}

/* !colorbox */

var colorbox = {

	init : function()
	{
		
		// flash and video (iframe/direct link to youtube)
		
		$(".youtube").colorbox({iframe:true, innerWidth:640, innerHeight:480, returnFocus:false});
		
		// elastic transition
		
		$(".image-overlay").colorbox();
	}

}

/* !ready event */

$(document).ready(function(){

	slider.init();
	homeRound.init();
	linkRound.init();
	portfolio_slider.init();
	preload.init();
	imageOverlay.init();
	checkbox.init();
	//colorbox.init();
	Shadowbox.init();
});
