var jq$ = jQuery.noConflict();



/*
	jQuery Coda-Slider v2.0 - http://www.ndoherty.biz/coda-slider
	Copyright (c) 2009 Niall Doherty
	This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
*/


jq$(function(){
	// Remove the coda-slider-no-js class from the body
	jq$("body").removeClass("coda-slider-no-js");
	// Preloader
	jq$(".coda-slider").children('.panel').hide().end().prepend('<p class="loading"></p>');
});

var sliderCount = 1;

jq$.fn.codaSlider = function(settings) {

	settings = jq$.extend({
		autoHeight: false,
		autoHeightEaseDuration: 1000,
		autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: false,
		autoSlideInterval: 7000,
		autoSlideStopWhenClicked: true,
		crossLinking: true,
		dynamicArrows: true,
//		dynamicArrowLeftText: "&#171; left",
//		dynamicArrowRightText: "right &#187;",
		dynamicTabs: true,
//		dynamicTabsAlign: "center",
		dynamicTabsPosition: "bottom",
		externalTriggerSelector: "a.xtrig",
		firstPanelToLoad: 1,
		panelTitleSelector: ".title",
		slideEaseDuration: 1000,
		slideEaseFunction: "easeInOutExpo"
	}, settings);

	
	
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		// alert("Testing preloader");
		
		var slider = jq$(this);
		
		// If we need a dynamic menu
		if (settings.dynamicTabs) {
			var dynamicTabs = '<div class="coda-nav" id="coda-nav-' + sliderCount + '"><ul></ul></div>';
			switch (settings.dynamicTabsPosition) {
				case "bottom":
					slider.parent().append(dynamicTabs);
					break;
				default:
					slider.parent().prepend(dynamicTabs);
					break;
			};
			ul = jq$('#coda-nav-' + sliderCount + ' ul');
			// Create the nav items
			jq$('.panel', slider).each(function(n) {
				ul.append('<li class="tab' + (n+1) + '"><a href="#' + (n+1) + '">' + jq$(this).find(settings.panelTitleSelector).html() + '</a></li>');												
			});
			navContainerWidth = slider.width() + slider.siblings('.coda-nav-left').width() + slider.siblings('.coda-nav-right').width();
//			ul.parent().css({ width: navContainerWidth });
//			switch (settings.dynamicTabsAlign) {
//				case "center":
//					ul.css({ width: (jq$("li", ul).width() + 2) * panelCount });
//					break;
//				case "right":
//					ul.css({ float: 'right' });
//					break;
//			};
		};
			
		// If we need a tabbed nav
		jq$('#coda-nav-' + sliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			jq$(this).bind("click", function() {
				navClicks++;
				jq$(this).addClass('current').parents('ul').find('a').not(jq$(this)).removeClass('current');
				currentVis();
				offset = - (panelWidth*z);
				alterPanelHeight(z);
				currentPanel = z + 1;
				jq$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				if (!settings.crossLinking) { return false } // Don't change the URL hash unless cross-linking is specified
				thisHashNum = jq$(this).attr('href').split("#");
				initSet(thisHashNum[1]);
			});
		});
		
		// If we need arrows
		if (settings.dynamicArrows) {
			slider.parent().addClass("arrows");
//			slider.before('<div class="coda-nav-left" id="coda-nav-left-' + sliderCount + '"><a href="#">' + settings.dynamicArrowLeftText + '</a></div>');
//			slider.after('<div class="coda-nav-right" id="coda-nav-right-' + sliderCount + '"><a href="#">' + settings.dynamicArrowRightText + '</a></div>');
			jq$('#coda-nav-' + sliderCount).after('<span class="coda-nav-right" id="coda-nav-right-' + sliderCount + '"><a href="#"><img src="/gallery/images/arrow_next.gif"></a></span>');
			jq$('#coda-nav-' + sliderCount).before('<span class="coda-nav-left" id="coda-nav-left-' + sliderCount + '"><a href="#"><img src="/gallery/images/arrow_prev.gif"></a></span>');
		};
		
		var panelWidth = slider.find(".panel").width();
		var panelCount = slider.find(".panel").size();
		var panelContainerWidth = panelWidth*panelCount;
		var navClicks = 0; // Used if autoSlideStopWhenClicked = true
		
		// Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
		jq$('.panel', slider).wrapAll('<div class="panel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		jq$(".panel-container", slider).css({ width: panelContainerWidth });
		
		// Specify the current panel.
		// If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
		if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			var currentPanel = parseInt(location.hash.slice(1));
			var offset = - (panelWidth*(currentPanel - 1));
			jq$('.panel-container', slider).css({ marginLeft: offset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) { 
			var currentPanel = settings.firstPanelToLoad;
			var offset = - (panelWidth*(currentPanel - 1));
			jq$('.panel-container', slider).css({ marginLeft: offset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var currentPanel = 1;
		};

			
		// Left arrow click
		jq$("#coda-nav-left-" + sliderCount + " a").click(function(){
			navClicks++;
			if (currentPanel == 1) {
				offset = - (panelWidth*(panelCount - 1));
				alterPanelHeight(panelCount - 1);
				currentPanel = panelCount;
				slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('li:last a').addClass('current');
			} else {
				currentPanel -= 1;
				alterPanelHeight(currentPanel - 1);
				offset = - (panelWidth*(currentPanel - 1));
				slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().prev().find('a').addClass('current');
			};
			currentVis();
			jq$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			if (settings.crossLinking) {
				location.hash = currentPanel;
				initSet(currentPanel);
			}; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		jq$('#coda-nav-right-' + sliderCount + ' a').click(function(){
			navClicks++;
			if (currentPanel == panelCount) {
				offset = 0;
				currentPanel = 1;
				alterPanelHeight(0);
				slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('a:eq(0)').addClass('current');
			} else {
				offset = - (panelWidth*currentPanel);
				alterPanelHeight(currentPanel);
				currentPanel += 1;
				slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().next().find('a').addClass('current');
			};
			currentVis();
			jq$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			if (settings.crossLinking) {
				location.hash = currentPanel;
				initSet(currentPanel);
			}; // Change the URL hash (cross-linking)
			return false;
		});
		
		
		// External triggers (anywhere on the page)
		jq$(settings.externalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (sliderCount == parseInt(jq$(this).attr("rel").slice(12))) {
				jq$(this).bind("click", function() {
					navClicks++;
					targetPanel = parseInt(jq$(this).attr("href").slice(1));
					offset = - (panelWidth*(targetPanel - 1));
					alterPanelHeight(targetPanel - 1);
					currentPanel = targetPanel;
					// Switch the current tab:
					slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (targetPanel - 1) + ') a').addClass('current');
					currentVis();
					// Slide
					jq$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
					if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
				});
			};
		});
			
		// Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
		if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			jq$("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
			currentVis();
			initSet(location.hash.slice(1));

		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
			jq$("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("current");
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			jq$("#coda-nav-" + sliderCount + " a:eq(0)").addClass("current");
			currentVis();
			initSet(1);
		};
		
		// Set the height of the first panel
		if (settings.autoHeight) {
			panelHeight = jq$('.panel:eq(' + (currentPanel - 1) + ')', slider).height();
			slider.css({ height: panelHeight });
		};
		
		// Trigger autoSlide
		if (settings.autoSlide) {
			slider.ready(function() {
				setTimeout(autoSlide,settings.autoSlideInterval);
			});
		};
		
		function alterPanelHeight(x) {
			if (settings.autoHeight) {
				panelHeight = jq$('.panel:eq(' + x + ')', slider).height()
				slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
			};
		};
		
		function autoSlide() {
			if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
				if (currentPanel == panelCount) {
					var offset = 0;
					currentPanel = 1;
				} else {
					var offset = - (panelWidth*currentPanel);
					currentPanel += 1;
				};
				alterPanelHeight(currentPanel - 1);
				// Switch the current tab:
				slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a').addClass('current');
				currentVis();
				// Slide:
				jq$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				setTimeout(autoSlide,settings.autoSlideInterval);
			};
		};
		
		// Kill the preloader
		jq$('.panel', slider).show().end().find("p.loading").remove();
		slider.removeClass("preload");
		
		sliderCount++;
		
	});
};


//----------------------------------------------------------
//----------------------------------------------------------
var codaSliderPanel;
var innerTxt;
var linkURL;

function setTxtUrl() {
	jq$('#topLayer').find('span').text(innerTxt);
	jq$('#bottomLayer').find('a').attr('href',linkURL);
}

function initSet(t) {
	nvHvNm = 0.4;
	navImg = jq$('#coda-nav-1 img');	
	codaSliderPanel = jq$('.panel');
		
	navImg.css({'opacity':nvHvNm});
	navImg.bind('mouseover',overCN).bind('mouseout',outCN);
	jq$('.current img').css({'opacity':1.0});
	jq$('.current img').unbind('mouseover').unbind('mouseout');
	
	function overCN(){jq$(this).stop().fadeTo(200, 0.9)};
	function outCN(){jq$(this).stop().fadeTo(450, nvHvNm)};	

	innerTxt = jq$(codaSliderPanel[t-1]).find('.bar_top').text();
	linkURL = jq$(codaSliderPanel[t-1]).find('.permalink').find('a').attr("href");
	
	setTxtUrl();
}

function currentVis() {
	if(jq$('li:first a','#coda-nav-1').hasClass('current') == true ){
		jq$('#coda-nav-left-1').css({'visibility':'hidden'});
	} else {
		jq$('#coda-nav-left-1').css({'visibility':'visible'});
	}
	
	if(jq$('li:last a','#coda-nav-1').hasClass('current') == true ){
		jq$('#coda-nav-right-1').css({'visibility':'hidden'});
	} else {
		jq$('#coda-nav-right-1').css({'visibility':'visible'});
	}
	if(jq$('#coda-nav-1 li').length == 1) {jq$('#coda-nav-1').css({'display':'none'})};
}


jq$(document).ready(function(){
//----------------------------------------------------------
var codaSlider1 = jq$('#coda-slider-1');
var hideBarInterval = 500;
var hideBarSpeed = 400;
var showBarSpeed = 250;
var AreaWidth = 665;
var AreaHeight = 401;
var barLayerTop = jq$('#topLayer');
var barLayerBot = jq$('#bottomLayer');
var barLayerBotHeight = 35;
var timerInBox;
var posX = new Array();
var posY= new Array();
var mouseX;
var mouseY;
var inPanelX;
var inPanelY;
var p = 0;

codaSlider1.codaSlider();
jq$('li:last', '#coda-nav-1').css({'margin-right':'0'});
//codaSliderPanel.mousemove(setTxtUrl);

function showBar() {
	barLayerTop.stop().css({'top':'0'}).fadeTo(showBarSpeed, 0.7);
	barLayerBot.stop().css({'top':AreaHeight - barLayerBotHeight + 'px'}).fadeTo(showBarSpeed, 0.7, function(){
			clearInterval(timerInBox);
			p = 100;
			timerInBox = setInterval(mousePos,hideBarInterval/2);
		}
	)
}

function hideBar() {
	clearInterval(timerInBox);
	//p = 0;
	barLayerTop.stop().fadeTo(hideBarSpeed, 0.0, function(){jq$(this).css({'top':- AreaHeight + 'px'})});
	barLayerBot.stop().fadeTo(hideBarSpeed, 0.0, function(){jq$(this).css({'top':AreaHeight + 'px'})});
}

jq$('html').bind('mousemove',function(e){
	mouseX = e.pageX;
	mouseY = e.pageY;
	inPanelX = mouseX - codaSlider1.offset().left;
	inPanelY = mouseY - codaSlider1.offset().top;

	if(inPanelX < 0 || AreaWidth < inPanelX  || inPanelY < 0 || AreaHeight < inPanelY) {
		hideBar();
	} else {
		clearInterval(timerInBox);
		timerInBox = setInterval(mousePos,hideBarInterval/2);
		showBar();
	}

//jq$('#xxx').html(mouseX + "/"+ inPanelX);
//jq$('#yyy').html(mouseY + "/"+ inPanelY);

});

function mousePos() {
	p ++;
	posX[p] = inPanelX;
	posY[p] = inPanelY;

//jq$('#pxxx').html(p + "/"+ posX[p]);
//jq$('#pyyy').html(p + "/"+ posY[p]);
	
	if(p > 108) {
		if(inPanelY < AreaHeight - barLayerBotHeight && posX[p-1] == posX[p] && posY[p-1] == posY[p]) {
			hideBar();
			codaSliderPanel.bind('mousemove',showBar);
		}
	}
		
}

//----------------------------------------------------------
});





