//jQuery Starter
$(document).ready(function() {
	$('<img src="/images/bg-dropdown_top.png">,<img src="/images/bg-dropdown_bot.png">');
	/***********************
	NAVIGATION EFFECTS
	***********************/
	$("#nav a:not(#nav .dropdown a)").hover(function(e) {
			$("#nav a").not(e.target).stop().animate({'color':'#d1d3d4'}, 400);
			$(e.target).stop().animate({'color':'#fff'}, 400);
	}, function() {
		$("#nav a:not(#nav .dropdown a)").stop().animate({'color':'#d1d3d4'}, 400);
	});	
	$("#navHome a:not(#nav .dropdown a)").hover(function(e) {
			$("#navHome a").not(e.target).stop().animate({'color':'#d1d3d4'}, 400);
			$(e.target).stop().animate({'color':'#fff'}, 400);
	}, function() {
		$("#navHome a:not(#nav .dropdown a)").stop().animate({'color':'#fff'}, 400);
	});	
	
	/***********************
	EMAIL SUBSCRIBE FORM
	***********************/
	$("#emailSubscribeForm").submit(function(event){
		if(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test($(this).find('#EMAIL').val())==false){
			event.preventDefault();
			$(this).append("<span class='error emailError'>Please enter a valid email</span>");
		}else{
			$(".emailError").remove();
		}
	/*	event.preventDefault();
		
		$.ajax({
			url:$("#emailSubscribeForm").attr('action'),
			type:'POST',
			data:$("#emailSubscribeForm").serialize(),
			dataType: 'jsonp',
			success: function(data){
				console.log(data);
				alert('You have been subscribed');
			}
		});
		*/
	});
	
	
	if ($('.page-gallery').length) {
		var sliderSize;
		function initThumbs(){
			var t = setInterval(function(){
				var sliderSize = $("#slider img").size();
				var thumbSize = $("#thumbGallery a").size();
				if ( thumbSize == sliderSize ) {
					clearInterval(t);
					$("#thumbGallery").fadeIn(600).paginate();
				}
			}, 100);
		}
		$(window).load(function() {
			$("#loadingGif").css('display', 'none');
			$("#photoSlideshow").fadeIn(600);
			var sliderSize = $("#slider img").size();
			$('#slider').show().cycle({
				fx: 'fade',
				speed: 'slow',
				timeout: 0,
				pager:  '#thumbGallery',
				// callback fn that creates a thumbnail to use as pager anchor
				pagerAnchorBuilder: function(index, slide) { 
					if (index+1==sliderSize) {initThumbs()}
					return '<a href="#" title="' + slide.alt + '"><div><img src="' + slide.src + '"></div></a>';
			    }
			});
			// $('#thumbGallery a').click(function(){
			// 	$('#thumbGallery a').removeClass('activeClass');
			// 	//$(this).addClass('activeSlide');
			// });
		});

		/***********************
		WRAP IN CHUNKS PLUGIN
		***********************/
		$.fn.wrapChildren = function(options) {
			var options = $.extend({
				childElem : undefined,
				sets : 1,
				wrapper : 'div'
			}, options || {});
			if (options.childElem === undefined) return this;
			return this.each(function() {
				var elems = $(this).children(options.childElem);
				var arr = [];
				elems.each(function(i,value) {
					arr.push(value);
					if (((i + 1) % options.sets === 0) || (i === elems.length -1))
					{
						var set = $(arr);
						arr = [];
						set.wrapAll($("<" + options.wrapper + ">"));
					}
				});
			});
		}
		/***********************
		PAGINATE PLUGIN
		***********************/
		jQuery.fn.paginate = function() {
		    //Wrap the thumbs into pages of 8 per page using the wrapChildren plugin (above)
			$('#thumbGallery').wrapChildren({
				childElem: 'a',
				sets: 15,
				wrapper: 'div class="page"'
			});
			$("#thumbGallery").cycle({
				fx: 'scrollHorz',
				speed: 300,
				timeout: 0,
				height: "305px",
				next: '#btnNext',
				prev: '#btnPrev',
				prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement){}
			});
		};
		// redefine Cycle's updateActivePagerLink function 
		$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
			$(pager).find('a').removeClass('activeSlide').filter('a:eq('+currSlideIndex+')').addClass('activeSlide');
		};
	}
});
/***********************
WRAP IN CHUNKS PLUGIN
***********************/

jQuery.fn.wrapInChunks = function(html, chunkSize) {

    chunkSize = chunkSize || 1;

    var items = this.get(),
        rows = [],
        cur = rows[0] = $(html);

    while (items[0]) {

        if (rows[rows.length - 1].children().length === chunkSize) {
            cur = rows[rows.length] = $(html);
        }

        cur.append( items.shift() );

    }

    return this.pushStack(rows);

};

/***********************
PAGINATE PLUGIN
***********************/
jQuery.fn.paginate = function() {
    //Wrap the thumbs into pages of 8 per page using the wrapInChunks plugin (above)
	$(".nivo-controlNav a").wrapInChunks('<div class="page" />', 15).appendTo('.nivo-controlNav');
	
	
	$(".nivo-controlNav").cycle({
		fx: 'scrollHorz',
		speed: 300,
		timeout: 0,
		height: "311px",
		next: '#btnNext',
		prev: '#btnPrev',
		prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement){
			/*if(isNext){
				if(zeroBasedSlideIndex+1 == $(".page").size() ){
					$("#btnNext").css('visibility', 'hidden');
				} else{
					$("#btnNext").css('visibility', 'visible');
					$("#btnPrev").css('visibility', 'visible');
				}
			} else{
				if(zeroBasedSlideIndex == 0 ){
					$("#btnPrev").css('visibility', 'hidden');
				}else{
					$("#btnPrev").css('visibility', 'visible');
					$("#btnNext").css('visibility', 'visible');
				}
			}*/
		}
	});
	
	/*
	//Make the first page the current page initially
	$(".nivo-controlNav .page:first").addClass('currentPage');
	
	//Set Some Variables to work with
	var totalPages = $(".nivo-controlNav .page").size();
	var currentPageNumber = $(".nivo-controlNav .currentPage").index();
	
	//Bring the Current Page to the Top
	$(".currentPage").css('zIndex', totalPages);
	
	
	//Add an event Handler for the Next Page Button
	$("#footer").click(function(){
		if(currentPageNumber+1 < totalPages){
			$(".nivo-controlNav .currentPage").removeClass('currentPage');
			$(".nivo-controlNav .page").eq(currentPageNumber+1).addClass('currentPage');
			$(".currentPage").css('zIndex', totalPages);
			currentPageNumber = currentPageNumber+1;
		}
	});
	*/


};






/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */

(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	// Some named colors to work with
	// From Interface by Stefan Petre
	// http://interface.eyecon.ro/

	var colors = {
		
	};
	
})(jQuery);
