/*----------------------------------------------------------------------
[Global JavaScript ] 
Desc: site wide javascript

Copyright:		Great Fridays 2010
Client:			x
Project:		x
Created by:		x
Created on:		x			
-----------------------------------------------------------------------*/

/* Global > replace fonts ---------------------------------------------*/

//Cufon.replace();


/* Not looking for any trouble here -----------------------------------*/

var $j = jQuery.noConflict();
var slideshowOpen = false;

/* Global > run some functions  ---------------------------------------*/

$j(document).ready(function() {
	
	//addBodyClass();
	//inputValueReplace();
	addBodyClass();
	slider();
	sideshow();	
	clientScroller();	
	
});


/* Global > add body classes -----------------------------------------*/

function addBodyClass() {
	
	// detect user agent
	var browser = "unknown";
	var version = navigator.userAgent.toLowerCase();

	// Internet Explorer
	if($j.browser.msie) {
		thisVersion = $j.browser.version.substring(0,1);
		browser = "ie" + thisVersion;
	};
	
	// Webkit
    if($j.browser.safari) browser = "webkit";
	
	// Mozilla
    if($j.browser.mozilla) browser = "mozilla";
	
	// Opera
    if($j.browser.opera) browser = "opera";
	
	// mobile safari - iPhone/iPod touch/iPad
	
	//to add
	
	// Add classes to body
	$j('body').addClass('js ' + browser);
};


/* Global > replace the default value of input fields -----------------*/

function inputValueReplace() {
  
	$j('input.valueReplace').each(function() { var o = new valueReplace($j(this)); });
	function valueReplace(e) {
		
		var o = {
		  init : function() {
				o.field = $j(e); 
				o.isSet = false;
				o.defaultValue = o.field.val();
				o.monitor();
			},
			monitor : function() {
				o.field.focus(function(){
					if( $j(this)[0].value == o.defaultValue) { $j(this)[0].value = ''; }
				});
				o.field.blur(function(){
					if($j(this)[0].value == '') { $j(this)[0].value = o.defaultValue;}
				});
			}
		};
		o.init();		
	};

}; 

/* Global > Portfolio Slider -----------------*/

function slider(){
		
	var scroller = $j('#scroller');
	var controls = $j('#controllers');
	controls.show();
	var container = $j('#container');
	
	var left = $j('#stepLeft', controls);
	var right =  $j('#stepRight', controls);
	
	var items = $j('ul', scroller);
	var total = items.length;
	
	var step = 960;
	var current = 0;
	
	/* add page links */
	var t ="";
	for ( i=1; i<=total; i++){
		t = '<li ';
		i == 1 ? t += 'class="disabled" ' : null; 
		t += '><a href="#"rel="' + i + '">0' + i + '</a></li>';
		right.before(t);
	};
	
	var quickLinks = $j('li:not(#stepLeft,#stepRight)',controls);
	
	/* Right stepper */
	right.children('a').click(function(e){
		e.preventDefault();
		if(slideshowOpen == true) return false;
		if(current!=total-1){
			left.hasClass('disabled2') ? left.removeClass('disabled2') : null;
			current++;
			animateScroller();
		};
	});
	
	/* Left stepper */
	left.children('a').click(function(e){
		e.preventDefault();
		if(slideshowOpen == true) return false;
		if(current!=0){  
			right.hasClass('disabled2') ? right.removeClass('disabled2') : null;
			current--;
			animateScroller();
		};
	});
	
	/* page jumper */
	quickLinks.click(function(e){
		e.preventDefault();
		if(slideshowOpen == true) return false;
		scrollTest();
		current = $j(this).children('a').attr('rel') -1;
		animateScroller();
	});
	
	
	/* animate scroller and test to add disabled classes */
	function animateScroller(){
		scroller.animate({"left": - (current * step) }, 500, 'easeInOutQuad');
		container.css({ left: (current * step) });		
		current == total-1 ? right.addClass('disabled2') : null;	
		current == 0 ? left.addClass('disabled2') : null;
		quickLinks.filter('.disabled').removeClass('disabled');
		//quickLinks.filter(' a[rel=' + (current+1) + ']').addClass('disabled');
		$j('a[rel=' + (current+1) + ']',controls).parent('li').addClass('disabled');
		
	};
	
	/* removing disabled class from active steppers */
	function scrollTest(){
		right.hasClass('disabled2') ? right.removeClass('disabled2') : null;
		left.hasClass('disabled2') ? left.removeClass('disabled2') : null;
	};
	
	
};


/* Global > slideshow ----------------------*/

function sideshow(){
	
	// vars
	
	var portfolio = $j('.portfolio');
	var container = $j('#container',portfolio);
	var loader = $j('#loader',portfolio);
	var controls = $j('#controllers');
	var scroller = $j('#scroller');
	
	// loop items 
	
	$j('#scroller li:not(.comingsoon)', portfolio).each(function() { var o = new setupSlideshow($j(this)); }).slideto({ target : '#portfolioHeader', speed  : 2000 });
	
	function setupSlideshow(e) {
		
		var o = {
			
			// init
			init : function() {
				o.trigger = e;
				o.location = o.trigger.find('a').attr('href');
				o.slideHeight = -586;
				o.events();
			},
			
			// event handlers
			events : function() {
				
				o.trigger.click(function(e){
					e.preventDefault();
										
					container.css({ bottom: o.slideHeight, top: 'auto' });			
					
					// show loader
					loader.show();
					
					container.load(o.location + '?' + Math.random()*99999 + ' #slideshow', function() { 				
						scroller.stop().animate({bottom: 586}, 500, 'easeInOutQuad');
						o.update(); 
						loader.hide();
						// hide controls
						controls.hide();
					});
				});
			
			},
			
			// loaded
			update : function() {
				
				slideshowOpen = true;
				o.imgContainer = $j('.imgarea',container);
							
				o.close = $j('#closeButton, #backButton, #imgHolder',container);
				o.close.click(function(e){
					e.preventDefault();
					o.destroy();
				});
				
				// slideshow logic
				o.imgItems = $j('ul li a', container);
				$j.preloadImages(o.imgItems.attr('href'));
				
				o.imgItems.each(function() { var o = new monitorImages($j(this)); });
				
				function monitorImages(iItem){
			
					iItem.click(function(e){
						
						o.imgItems.filter('.active').removeClass('active');
						
						e.preventDefault();
						o.imgTrigger = iItem;
						o.href = o.imgTrigger.attr("href");
						o.imgTrigger.addClass('active');
						
						// animate current image out, bring our image in
						o.imgContainer.find('img').stop().fadeOut('fast', function() {	
							o.imgContainer.children('#imgHolder').empty()
								.append("\<img src='" + o.href + "'\/>").children('img').css({ opacity: 0 }).stop().animate({opacity: 0},800).animate({opacity: 1},300);
						});
						
					});

				};
								
			},
			
			// destroy slideshow
			destroy : function() {
				slideshowOpen = false;
				// animate slideshow out
				scroller.stop().animate({bottom: 0}, 500, 'easeInOutQuad', function(){
					container.empty();															
				});
				
				controls.show();

			}
			
		};
		
		o.init();
		
	};
	
};

/* client list scroller */
function clientScroller() {

	$j('#clientScroller').each(function() { var o = new setupScroller($j(this)); });

	function setupScroller(e) {

		var o = {
			init: function() {
				o.wrapper = $j(e);
				o.controls = o.wrapper.next('.clientPaging');
				o.items = o.wrapper.children('li');
				o.total = o.items.length;
				//o.outside = o.total - 12; 
				o.itemsHeight = o.wrapper.height();
				//console.log(o.itemsHeight);
				//o.outsideHeight = o.itemsHeight * o.outside;
				if (o.itemsHeight > 330) o.setUpScroller();
			},
			setUpScroller: function() {
				o.controls.show();
				o.clientInfo = o.controls.find('.clientInfo');
				o.clientInfo.hide();
				o.up = o.controls.find('.upLink');
				o.down = o.controls.find('.downLink');
				o.wrapper.wrap("<div id='mask'></div>");
				o.selected = o.wrapper.children('.selected');
				o.controls.find('.clientInfo span:last-child').html(Math.ceil(o.total / 14));

				//o.maxHeight = 282;

				/* Step height to scroll */
				o.scrollerHeight = 330;
				//o.move = 312;
				/* Current page */
				o.step = Math.ceil((o.items.index(o.selected) + 1) / 14);
				o.controls.find('.clientInfo span:first-child').html(o.step);
				o.position = -(o.step - 1) * o.scrollerHeight;
				o.mask = $j('#mask').css('height', o.scrollerHeight);
				o.wrapper.css('top', o.position);

				if (o.step == 1) {
					o.up.addClass('disabled');
				} else if (o.step == Math.ceil(o.total / 14)) {
					o.down.addClass('disabled');
				};

				o.down.click(function(e) {
					if (!$j(this).hasClass('disabled')) o.scrollDown();
				});
				o.up.click(function(e) {
					if (!$j(this).hasClass('disabled')) o.scrollUp();
				});
			},
			scrollDown: function() {
				if (o.up.hasClass('disabled')) o.up.removeClass('disabled');
				
				var step = (o.step > 1)? 14 : 0;
				var numToScroll = (o.total - 14) - (step * (o.step - 1));
				if (numToScroll > 14) numToScroll = 14;
				
				o.position = o.position - (numToScroll * (o.scrollerHeight / 14));
				o.wrapper.animate({ top: o.position }, 500, 'easeInOutQuad');
				o.step++;
				o.controls.find('.clientInfo span:first-child').html(o.step);
				if (o.step == Math.ceil(o.total / 14)) o.down.addClass('disabled');
			},
			scrollUp: function() {
				if (o.down.hasClass('disabled')) o.down.removeClass('disabled');
				
				var step = (o.step == 2)? 0 : 14;
				var numToScroll = (o.total - 14) - (step * (o.step - 2));
				if (numToScroll > 14) numToScroll = 14;
				
				o.position = o.position + (numToScroll * (o.scrollerHeight / 14));
				o.wrapper.animate({ top: o.position }, 500, 'easeInOutQuad');
				o.step--;
				o.controls.find('.clientInfo span:first-child').html(o.step);
				if (o.step == 1) o.up.addClass('disabled');
			}
		};
		o.init();
	};

};

/* Global > preload images ---------------------------------------------------------------------------*/
/* Dependencies - none */

jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i >= 0; i--) {
		jQuery("<img>").attr("src", a[i]);
	}
}

/* Global > custom easing ---------------------------------------------*/

$j.easing.quad = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
};

$j.easing.easeInOutQuad = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
};
