//-------------------------------//

//-->RiserDesign<-- 

// 

// jQuery Image changer with Navigation

// 

// Created: 5/10/2010

//

// Contact Email: help@riserdesign.com

//

// **Requires jQuery Library

// http://docs.jquery.com/Downloading_jQuery

//

//-------------------------------//

var conName;

	function imageSwitch() {

		//Set all properties here//-------------->

		this.sContainerName = 'slides-wrapper';//Id of element wrapping images
		
		conName = this.sContainerName;

		this.iDuration = '5000';//Image duration in milliseconds

		this.bNavigation = false;//Set true for navigation links for the images

		this.bNumText = true;//If true navigation will be numbered

		this.oSelf = this;//Object reference for scope // Leave unaltered

		//--------------------------------------->

	}

	

	//**prepareImages method//------------------>

	/////////////////////////////////////////

	imageSwitch.prototype.prepareImages = function() {

		//Get holder element

		this.vHolder = $("#" + this.sContainerName);

		//Get all images within the holder

		var images = $("#" + this.sContainerName + " a");

		images.each(function() {

				if($(this).attr('href') == '#') {

					$(this).addClass('no-link');	

				}

		});

		//Set image count

		this.iImageCount = images.length;

		//Wrap all images in div's with the first one having the class 'current'

		for(var i =0; i < images.length; i++ ) {

			if(i == 0) {

				this.vHolder.prepend($("<div class='current'>").append(images[i]));	

			} else {

				this.vHolder.append($("<div>").append(images[i]));	

			}

		}//end for

		//Set vars for setInterval scope resolution

		var obj = this.oSelf;

		var duration = this.iDuration;			

		//Initiate nav addition if set

		if(this.bNavigation) {

			this.addNav(obj);

		}//end if

		

		//Start Image Switching initial behavior

		this.iTheInt = setInterval(function() { switchImage(obj); },duration);

		images.click(function() {

			if($(this).attr('href') == '#') {

				return false;

			} 

		});

	}//end prepareImages method

	

	//**switchImage method//------------------>

	/////////////////////////////////////////

	function switchImage(obj) {

		

		//get current image

		var curImg = $("#" + conName + " div.current");

		//get the next image

		var nxtImg = curImg.next();

		//but if this is already the last image make the first image be next

		if(nxtImg.length == 0) {

			nxtImg = $("#" + conName + " div:first");

		}//if

		//remove current from current image and add previous

		curImg.removeClass("current").addClass("previous");

		//set next image to current and fade in

		nxtImg.css({opacity: 0.0}).addClass("current").animate({opacity: 1.0},1000,function() {curImg.removeClass("previous");});

		if(obj.bNavigation) {

		//advance active link if nav is set

		obj.linkSwitch();

		

		}

	}//end switchImage method

	

	//**addNav method//----------------------->

	/////////////////////////////////////////

	imageSwitch.prototype.addNav = function(obj) {

			//Insert ul

			this.vHolder.prepend($("<ul id='list1'>").fadeTo(500,.75));

			//for loop to create li items and anchors and add them to ul 

			for(var i = 0; i < this.iImageCount; i++) {

				  //if on the last image

				  if(i==0) {

					//if numText is true add numbers to links 

					if(this.bNumText) {

						$("#list1").append($("<li class='active'>").append($("<a href='"+i+"'>").text(i+1)));

					} else {

				  	$("#list1").append($("<li class='active'>").append($("<a href='"+i+"'>")));

				  	} } else {

						//if numText is true add numbers to links

						if(this.bNumText) {

							$("#list1").append($("<li>").append($("<a href='"+ i +"'>").text(i+1)));

						} else {

					  $("#list1").append($("<li>").append($("<a href='"+ i +"'>")));

				  	}

					}//else

				  

			  	}//for

			/*$("#list1").append($("<li class='show-control pause'>"));

			$("li.show-control").bind("click",function() {

							if($(this).hasClass('pause')) {

								clearInterval(obj.iTheInt);

							} else {

								obj.iTheInt = setInterval(function() { switchImage(obj); },duration);

							}

							$(this).toggleClass('pause');						 

													 });*/



			//Setup link behavior when clicked

			$("#list1 a").bind("click",function(evt) {												

				//get the parent li for the link that was just clicked

				var it = $(this).parent();

				//get link href to use for li index

				var url = $(this).attr("href");

				//remove active from where ever it is and add it to the li that was clicked

				$("#" + conName + " li.active").removeClass("active");

				it.addClass("active");

				//begin image change callback function

				obj.changeImg(url);	

				//Make sure link default behavior is cancelled

				return false;

			});//end onload function

			//set in the middle of parent

			/*var w =	$("#list1").width() / 2;

			var pw = $("#list1").parent().width() / 2;

			var l = pw - w;

			$("#list1").css({left:l+'px'});*/

	}//end addNav method

	

	//**changeImg method//------------------>

	/////////////////////////////////////////

	imageSwitch.prototype.changeImg = function(url) {

		//stop image switching for now

		clearInterval(this.iTheInt);

		//get the div of the li that was clicked

		var imgGet = $("#" + conName + " div:eq("+url+")");

		//get the current image

		var curImg2 = $("div.current");

		//set current image to previous

		curImg2.removeClass("current").addClass("previous");

		//set selected image to current and fade in then remove previous from old image

		imgGet.css({opacity: 0.0}).addClass("current").animate({opacity: 1.0},300,function() {curImg2.removeClass("previous")});									 		//Set vars for setInterval scope resolution

		duration = this.iDuration;

		obj = this.oSelf;

		//restart image switching

		obj.iTheInt = setInterval(function() { switchImage(obj); },duration);

		

	}

	

	//**linkSwitch method//------------------>

	/////////////////////////////////////////

	imageSwitch.prototype.linkSwitch = function() {

		//get currently active link

		var link1 = $("#list1 li.active");

		//get the next link

		var link2 = link1.next();

		//but if this is the last link then make the first link next

		if(link2.length == 0) {

			link2 = $("#list1 li:first");

		}//if

		//remove active from current

		link1.removeClass("active");

		//make next active

		link2.addClass("active");			

	}

	

//Define new object and call prepareImages method	
var isHomePage = false;
$(function() {	
		navBuilder();
		var myImages = new imageSwitch();

		myImages.prepareImages();
		
		path = location.pathname;
		if (path == '/' || path == '/Default.asp') {
			//home page	
			isHomePage = true;
			$("body").addClass('home');
		}
		if(! isHomePage ) {
		table = $("#content table:first-child");
		width = table.width();
		if ( width > '800') {
			$("#left-col").hide();
			$("#content").css({ width : '939px' });
		}
		}
		
		
		//$(".milonictable").css({ width : '670px' });
		

		  });
		  
$(document).ready(function(e) {
    //check if cart
	if(isHomePage) {
	cont = $("#content");
	features = $("#content table");
	
	var products;
	
	$.each(features, function() {
		if($(this).attr('width') == '96%') {
			products = $(this);
		}
	});
	
	ads = $("#ad-wrapper");
	ads.insertBefore('#left-col');
	
	cont.html(products);
	cont.prepend($('<h2 class="feature">Featured Products</h2>'));
	}
	
	
	
	//Top menu
		$("#menu0").css({ width : '670px', color: 'red' });

});

function navBuilder() {
	//Build nav
	var mainNav = $("<ul id='main-nav'>");
	//gather nav
	var navParent = $("#display_menu_1 table.nav");
	var links = [];
	var linkelems = navParent.find('td');
	var lastLink = false;
	var linkMode = 'main'; // or sub
	
	$.each(linkelems, function(k,v) {
		//cur ele
		var vj = $(v);
		//link
		var a = vj.find('a');
		
		if ( !vj.hasClass('subnav') ) {
			//Main cat
			link = $("<li class='main link_" + k + "'>").append(a);
			mainNav.append(link);
			lastLink = link;
		} else {
			//Sub cat
			var ul = lastLink.find('ul'); 
			if ( ul.length < 1 ) {
				ul = $("<ul class='sub'>");
				lastLink.append(ul);
			}
			link = $("<li class='sub link_" + k + "'>").append(a);
			ul.append(link);
		}
		//console.log(vj.attr('class'));
	});
	$("#top_nav").prepend(mainNav);
	
	$('#main-nav li ul').parent().die('hover').hover(function() {
			var $this = $(this);
			var parentWidth = $this.width();
			$this.find('ul li').css({
				minWidth : parentWidth + 'px'
			});
			$this.find('ul').fadeIn();
		},function() {
			var $this = $(this);
			$this.find('ul').fadeOut();
		});
}
