// RUN ON DOM LOAD
$(function() {
	// reset textbox values on refresh
	$(".search > .textbox, .newsletter > .textbox.email, .newsletter > .textbox.join").attr("value", "");

	$(".search > .textbox").watermark('watermark', 'Search Clintar.com');			
	$(".newsletter > .textbox.email").watermark('watermark', 'Enter your email...');
	$(".newsletter > .textbox.join").watermark('watermark', 'Join our newsletter!');
	$(".newsletter > .textbox.join").mouseover(function() {
		$(this).hide();
	});

	var hasFocus = false;
	$(".newsletter > .textbox.email").focus(function() {
		hasFocus = true;
	});
	$(".newsletter > .textbox.email").blur(function() {
		hasFocus = false;
		$("form.newsletter > .textbox.join").mouseover().show();
	});
	$(".newsletter > .textbox.email").mouseout(function() {
		if (!hasFocus)
			$(".newsletter > .textbox.join").mouseover().show();
});
    	
	$(".newsletter > a, .sec-nav > a.sec-newsletter").fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	400, 
		'speedOut'		:	200, 
		'overlayOpacity' : 0.7,
		'overlayColor' : '#333',
		'overlayShow'	:	true,
		'autoScale'		:	false
	});
	
	$("#newsletter-data input.textbox").watermark('watermark', $(this).attr("type"));
});

function slideShow() {
	// HOME PAGE SLIDESHOW CODE //
	// set first slide as selected
	$(".slideshow ul li:first").addClass("selected");
	$(".slideshow .slide-nav a:first").addClass("selected");
	
	
	/* generate slide nav buttons
	$(".slideshow ul li").each(function(index) {
		$(".slideshow .slide-nav").append("<a></a>");
	});
	$(".slideshow .slide-nav a:first").addClass("selected");
	*/
	
	
	/* show paging buttons on mouseover
	$(".slideshow").mouseover(function() {
		$(".slideshow > a.next").fadeIn(250);
		$(".slideshow > a.prev").fadeIn(250);
	});
	$(".slideshow").mouseleave(function() {
		$(".slideshow > a.next").fadeOut(250);
		$(".slideshow > a.prev").fadeOut(250);
	});
	*/

	
	// AUTO CHANGE SLIDE
	var t;
	var timer_is_on = 0;
	var getTimer;
	var isFirstTime = true;
	
	// starts the auto changer
	doTimer();
	
	function autoChangeSlide() {
	    if (!isFirstTime) {
	        var slideLength = $(".slideshow ul li").length;

	        // determine the index of the selected slide
	        $(".slideshow ul li").each(function(index) {
	            if ($(this).hasClass("selected")) {
	                thisSlide = index;
	            }
	        });
	        if (thisSlide != null) {
	            if (thisSlide + 1 > slideLength - 1)
	                goToSlide(0);
	            else
	                goToSlide(thisSlide + 1);
	        }
	    }
		
		isFirstTime = false;
			
		t = setTimeout(function() {
			autoChangeSlide();
		}, 10000);
	}

	// starts the javascript timer
	function doTimer() {
		if (!timer_is_on) {
			timer_is_on = 1;
			autoChangeSlide();
		}
	}

	// stops the javascript timer
	function stopCount() {
		clearTimeout(t);
		timer_is_on = 0;
	}

	
	// EVENT HANDLERS
	// click next
	$(".slideshow > a.next").click(function() {
		stopCount(); //stop auto change slides
		var slideLength = $(".slideshow ul li").length;
	
		// determine the index of the selected slide
		$(".slideshow ul li").each(function(index) {
			if ($(this).hasClass("selected")) {
				thisSlide = index;
			}
		});
		
		if (thisSlide + 1 > slideLength - 1)
			goToSlide(0);
		else
			goToSlide(thisSlide + 1);
	});
	
	
	// click prev
	$(".slideshow > a.prev").click(function() {
		stopCount(); //stop auto change slides
		var slideLength = $(".slideshow ul li").length;
	
		// determine the index of the selected slide
		$(".slideshow ul li").each(function(index) {
			if ($(this).hasClass("selected")) {
				thisSlide = index;
			}
		});
		
		if (thisSlide - 1 < 0)
			goToSlide(slideLength - 1);
		else
			goToSlide(thisSlide - 1);
	});
	
	
	// slide nav click
	$(".slide-nav > a").click(function() {
		stopCount(); //stop auto change slides
		var slideIndex = $(".slide-nav > a").index(this);
		goToSlide(slideIndex);
	});
					

	// HELPER FUNCTIONS
	// function to go to a specific slide index
	function goToSlide(index) {
		var thisSlide = 0;
		
		// determine the index of the selected slide
		$(".slideshow ul li").each(function(index) {
			if ($(this).hasClass("selected")) {
				thisSlide = index;
			}
		});
		
		// remove selected class from current slide.
		$(".slideshow ul li").eq(thisSlide).removeClass("selected");
		$(".slideshow .slide-nav a").eq(thisSlide).removeClass("selected");
		
		// add selected class to next slide.
		$(".slideshow ul li").eq(index).addClass("selected");
		$(".slideshow .slide-nav a").eq(index).addClass("selected");
		
		// adds a custom trigger event which is then bound to each slide
		$(".slideshow ul li").trigger("selectedClassChanged", [index]);
		
		//alert("index: " + index + "\nthisSlide: " + thisSlide);
		var moveDistance = index * -960;				
		$(".slideshow ul").animate({left: moveDistance}, 500);
	}
}


function franOppSlideShow() {
	// HOME PAGE SLIDESHOW CODE //
	// set first slide as selected
	$(".slideshow ul li:first").addClass("selected");
	$(".slideshow .slide-nav a:first").addClass("selected");
	
	// AUTO CHANGE SLIDE
	var t;
	var timer_is_on = 0;
	var getTimer;
	var isFirstTime = true;
	
	// starts the auto changer
	doTimer();
	
	function autoChangeSlide() {
		if (!isFirstTime) {
			var slideLength = $(".slideshow ul:first > li").length;
	
			// determine the index of the selected slide
			$(".slideshow ul:first > li").each(function(index) {
				if ($(this).hasClass("selected")) {
					thisSlide = index;
				}
			});
			
			if (thisSlide + 1 > slideLength - 1)
				goToSlide(0);
			else
				goToSlide(thisSlide + 1);
		}
		
		isFirstTime = false;
			
		t = setTimeout(function() {
			autoChangeSlide();
		}, 10000);
	}

	// starts the javascript timer
	function doTimer() {
		if (!timer_is_on) {
			timer_is_on = 1;
			autoChangeSlide();
		}
	}

	// stops the javascript timer
	function stopCount() {
		clearTimeout(t);
		timer_is_on = 0;
	}

	
	// EVENT HANDLERS
	// slide nav click
	$(".slide-nav > a").click(function() {
		stopCount(); //stop auto change slides
		var slideIndex = $(".slide-nav > a").index(this);
		goToSlide(slideIndex);
		return false;
	});
	
	 $(".slideshow a.more").click(function() {
		stopCount();
	 });
					

	// HELPER FUNCTIONS
	// function to go to a specific slide index
	function goToSlide(index) {
		var thisSlide = 0;
		
		// determine the index of the selected slide
		$(".slideshow ul:first > li").each(function(index) {
			if ($(this).hasClass("selected")) {
				thisSlide = index;
			}
		});
		
		// hide more-content
		$(".slideshow .more-content").hide();
		
		// remove selected class from current slide.
		$(".slideshow ul:first > li").eq(thisSlide).removeClass("selected");
		$(".slideshow .slide-nav a").eq(thisSlide).removeClass("selected");
		
		// add selected class to next slide.
		$(".slideshow ul:first > li").eq(index).addClass("selected");
		$(".slideshow .slide-nav a").eq(index).addClass("selected");
		
		// adds a custom trigger event which is then bound to each slide
		$(".slideshow ul:first > li").trigger("selectedClassChanged", [index]);
		
		//alert("index: " + index + "\nthisSlide: " + thisSlide);
		var moveDistance = index * -590;	
		$(".slideshow ul:first").stop(true, false);
		$(".slideshow ul:first").animate({left: moveDistance}, 500);
	}
}
