// define on load font size (in %)
var fontSize = 100;

// make jQuery no conflic with other APIs
var $j = jQuery.noConflict();

var ran = Math.floor(Math.random() *3);

var imageWidth = 0;
var docWidth;
var kleft;
var currLeft;
var currTop = 0;
var speed = 1000;

$j(document).ready(function(){


	// Define easing type
	//jQuery.easing.def = 'easeOutSine';
	
	/*
		* add fancybox video links
	*/
	$j(".video-link").fancybox({
		'width'             : 640,
		'height'            : 390,
	    'padding'           : 0,
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'              : 'swf',
		'swf' 	            : {
			'wmode'		: 'transparent',
			'allowfullscreen'	: 'true'
		}
	});
	
	$j("a.sbox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
	
	/*
		Add carousel functionality
	*/
    $j(".featured-carousel").jCarouselLite({
    	scroll: 1,
    	visible: 3,
    	start: 0,
    	speed: 750,
    	auto: 6000,
        btnNext: ".next-link",
        btnPrev: ".prev-link"
       /* easing: "easeOutSine"*/
    });
    
    
    /*
		Add kayak carousel functionality
	*/
    $j(".kayak-carousel").jCarouselLite({
    	/*scroll: 3,
    	visible: 3,*/
    	start: 0,
    	speed: 750,
        btnNext: ".next-link",
        btnPrev: ".prev-link",
        circular: false
        /*easing: "easeOutSine"*/
    });
    
    //setFeaturedInfo("item" + (ran+1));
	
	/*
		* open links in _blank window if tagged with external 
	*/
	$j('a[rel*=external]').click(function(){
  		this.target = "_blank";
	})
	
	
	// ************* DEFINE - Roll over events for any image tagged with .over
	$j('img.over').each(function() 
	{
    	var t = $j(this);
    	var src = t.attr('src'); // initial src
    	var path = src.substring(0,src.lastIndexOf('/')); // path to file
    	var filename = src.substring(src.lastIndexOf('/'), src.lastIndexOf('.')); // let's get file name without extension
    	
    	t.hover(function(){
        	$j(this).attr('src', path + filename + '-over.' + /[^.]+$/.exec(src)); //last part is for extension 
        	
        	//alert(path+filename+ '-over.' + /[^.]+$/.exec(src));
        	
    	}, function(){
        	$j(this).attr('src', path + filename + '.' + /[^.]+$/.exec(src)); //removing '-over' from the name
        	
        	//alert(path + filname + '.' + /[^.]+$/.exec(src));
    	});
	});
	
	
	var config = {    
     	over: showSub, // function = onMouseOver callback (REQUIRED)    
     	timeout: 700, // number = milliseconds delay before onMouseOut    
     	out: hideSub // function = onMouseOut callback (REQUIRED)    
	};
	
	var config_sub = {    
     	over: showTer, // function = onMouseOver callback (REQUIRED)    
     	timeout: 1000, // number = milliseconds delay before onMouseOut    
     	out: hideTer // function = onMouseOut callback (REQUIRED)    
	};
	
	$j("a.main-link").click(function(event) {
		event.preventDefault();
	});
	
	$j("a.sub-link").click(function(event) {
		if($j(this).attr('href') == "#") event.preventDefault();
	});
	
	
	$j("a.main-link").hoverIntent( config );
	$j('a.sub-link').hoverIntent( config_sub );
	
	//$j("a[rel*=toplevel]").hoverIntent( config );
	//$j("a[rel*=sublevel]").hoverIntent( config_sub );
	
	
	$j("#container-top").mousemove(function(e){
      
		var relativeX = e.pageX - this.offsetLeft;
    	var relativeY = e.pageY - this.offsetTop;
    	
    	if((relativeX >= 200 && relativeX <= 900) && (relativeY >= 50 && relativeY <= 180)) {
    		//$j("#page-cord").text("in menu");
    	}
    	else {
    		//$j("#page-cord").text("out of menu " + timeout);
    		showCurrent();
    		
    	}
      //$j("#client-cord").text("( relativeX, relativeY ) - " + relativeX + " / " + relativeY);
    });

	
	/* Create catch all to close show current menu 
	$j("ul.submenu").mouseleave(function() {
		//timeout = window.setTimeout(showCurrent,2000);
		
		alert("moved out of menu");
	});*/
	
	
	//$j("li.menu-item").hover( showArrow, removeArrow );
	
	//setTimeout("showHeading()", 500);
	
	
	/*var config_filter = {    
     	over: dropOpen,    
     	timeout: 750,    
     	out: dropClose  
	};

	$j("li.filter-item").hoverIntent( config_filter );*/
	
	
	/* PRODUCT PAGES 
	$j("div.product").hover( showBorder, removeBorder );
	$j("div.product").click(function(){

  		 Shadowbox.open( {player:'inline', content:"#sb-html", width:586, height:506 } );
	});*/
	
	
	var config_filter = {    
     	over: dropOpen,    
     	timeout: 750,    
     	out: dropClose  
	};

	$j("li.filter-item").hoverIntent( config_filter );
	
	
	$j(".infobox-close").click(function(){
		$j(this).parent().hide();
	});
	
	
	/*
		* infobox control
	*/
	$j('ul.filter-options li a[rel*=color]:first').each(function(){
  		$j('#kayak-wrapper').css('background-image', "url(" + $j(this).attr("href") + ")");
  		
  		$j('#other-wrapper').css('background-image', "url(" + $j(this).attr("href") + ")");
  		
  		var tmpImg = new Image() ;
    	tmpImg.src = $j(this).attr("href");
    	tmpImg.onload = function() {
        	// Run onload code.
        	//alert("imagewidth is: " + this.width + " height: " + this.height);
        	
        	imageWidth = this.width;
        	
        	// set background pos of kayak image
			defineBackPos();
        	setBackPos('center',currTop);
    	};

	})
	
	$j(".filter-title").click(function(event) {
		var f = $j(this).parent().find('ul.filter-options li:first-child a').click();
	});
	
	$j("ul.filter-options li a").click(function(event) {
	
		var rel = $j(this).attr("rel");
		
		// hide all the info boxes
		$j('.infobox').hide();
		
		if(rel == "color") {
			//prevent default click
			event.preventDefault();
			
			$j('#kayak-wrapper').css('background-image', "url(" + $j(this).attr("href") + ")");
		} 
		else if(rel == "external") {
			// do nothing. catch all case above
			this.target = "_self";
		}
		else if(rel == "zoom") {
			//prevent default click
			event.preventDefault();
			
			createIframe($j(this).attr("href"));
		}
		else {	
			//prevent default click
			event.preventDefault();
			
			// deal with imperial/metric
			if($j(this).html() == "Imperial") {
				$j('.imperial').show();
				$j('.metric').hide();
			} else {
				$j('.imperial').hide();
				$j('.metric').show();
			}
		
			// show the current one
			$j("#" + rel).fadeIn();
		}
	});
	
	$j("a.gear-link").click(function(event) {
		//prevent default click
		event.preventDefault();
		
		// hide all the info boxes
		$j('.infobox').hide();
		
		// show the current one
		$j("#infobox-gear").fadeIn();
	});
	
	/*$j("a.video-link").click(function(event) {
		//prevent default click
		event.preventDefault();
		
		alert("Show a video");
	});*/
	
	/*$j("a.zoom-link").click(function(event) {
		//prevent default click
		event.preventDefault();
		
		//alert("Show zoom image");
		
		var o = $j('#kayak-wrapper');
		var p = o.css("background-position");
		
		currLeft = (currLeft == undefined) ? kleft : currLeft;
	
		o.css({backgroundPosition: p}) // workaround for FF 2.0 bug
		o.stop().animate({backgroundPosition: currLeft + 'px -345px'},750);
	});*/
	
	$j('a.zoom').click(function() { createIframe() } );
	
	
	/*$j('a.zoom-link').toggle(function() {
		$j(this).html("TOP VIEW");
		setBackPos('none',-345)
	}, function() {
		$j(this).html("SIDE VIEW");
		setBackPos('none',0)
});*/
	
	
	/*$j("a.bg-slide").click(function(event) {
		//prevent default click
		event.preventDefault();
		
		var p = $j("#info-container").position();
		
		$j("#kayak-wrapper").css( {backgroundPosition: "-20px 35px"} )
		$j("#kayak-wrapper").stop().animate({backgroundPosition:"(-20px 94px)"}, {duration:500});
		
		//$j("#kayak-wrapper").css("background-position" , (p.left + "px 40px"));
	});*/

var otherX = 0;
	
$j('#other-wrapper #slide-right').click(function(event) {
	//prevent default click
	event.preventDefault();
	
	var o = $j('#other-wrapper');
	
	// set the css
	o.css({backgroundPosition: otherX + 'px ' + '0px'}) // workaround for FF 2.0 bug
	
	otherX -= 625;
	o.stop().animate({backgroundPosition: otherX + 'px ' + '0px'},speed);
});

$j('#other-wrapper #slide-left').click(function(event) {
	//prevent default click
	event.preventDefault();
	
	var o = $j('#other-wrapper');
	
	// set the css
	o.css({backgroundPosition: otherX + 'px ' + '0px'}) // workaround for FF 2.0 bug
	
	otherX += 625;
	o.stop().animate({backgroundPosition: otherX + 'px ' + '0px'},speed);
});


	
$j('#kayak-wrapper #slide-left').click(function(event) {
	//prevent default click
	event.preventDefault();

    setBackPos("right",currTop);
});

/*$j('#slide-left').mouseup(function() { 
    $j('#kayak-wrapper').stop();
});*/

$j('#kayak-wrapper #slide-right').click(function(event) {
	//prevent default click
	event.preventDefault();
    setBackPos("left",currTop);
});

/*$j('#slide-right').mouseup(function() { 
    $j('#kayak-wrapper').stop();
});*/
	
	
	/*$j('#kayak-wrapper')
				   .css({backgroundPosition: '50% 0'}) // workaround for FF 2.0 bug
				   .click(function(){
	                   $j(this)
							.animate({backgroundPosition: '-20px 250px'});
	               });
	
	$(this).stop().animate({backgroundPosition:"(-20px 94px)"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(40px 35px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-20px 35px"})
			}})
	*/
	
	
	$j(window).resize(function() {
		defineBackPos();
		setBackPos('resize',currTop);
	});
	
});

var t_ = 0;

function defineBackPos() 
{
	docWidth = $j(document).width();
	kleft = Math.floor((docWidth - (imageWidth/2)) / 2);
	currLeft = kleft;
}

function setBackPos(l,t)
{
	if($j('#kayak-wrapper').attr('class') == "no-slide") return;
	
	// create objects
	var o = $j('#kayak-wrapper');
	var p = o.css("background-position");
	
	// get offset
	var m = $j("#info-container").offset();
	
	t_ += 1000;
	
	// set the css
	o.css({backgroundPosition: p}) // workaround for FF 2.0 bug
	
	//alert(t + " / " + currTop + " / currLeft: " + currLeft);
	
	// deal with top position first
	if(currTop != t) {
		o.stop().animate({backgroundPosition: currLeft +'px ' + ((t == 0) ? t : (t + 'px'))},speed);
		currTop = t;
	}
	
	// deal with left movement
	if(l == 'left') {
		//var offset = m.left - (imageWidth - $j("#info-container").width());
		currLeft -= 750;
		o.animate({backgroundPosition: currLeft +'px ' + ((currTop == 0) ? currTop : (currTop + 'px'))},speed);
		//currLeft = offset;
	}
	
	// deal with right movement
	if(l == 'right') {
		
		currLeft += 750;
		o.stop().animate({backgroundPosition: currLeft +'px ' + ((currTop == 0) ? currTop : (currTop + 'px'))},speed);
		//currLeft = m.left;
	}
	
	// deal with resize movement
	if(l == 'resize') {
		o.stop().animate({backgroundPosition: kleft +'px ' + ((currTop == 0) ? currTop : (currTop + 'px'))},speed);
	}
	
	// deal with centering
	if(l == 'center') {
		o.css({backgroundPosition: kleft +'px ' + ((currTop == 0) ? currTop : (currTop + 'px'))},speed);
	}

}

// filter dropdown
function dropOpen() {
	var desc = $j(this).find("ul.filter-options");
	desc.slideDown('fast');
}

function dropClose() {
	var desc = $j(this).find("ul.filter-options");
	desc.slideUp('fast');
}


/*function showHeading() {
	$j(".main-header").css("visibility","visible");
}*/

var timeout = 0;
var curSub = null;

function showSub()
{
	var sub = $j(this).parent().find("ul.submenu");
	
	sub.removeClass("add-border");

	// hide sub and ter menus
	$j("ul.submenu").hide();
	
	if(sub.css('display') == "none")
	{
		//$j("ul.termenu").hide();
		sub.fadeIn("fast");
	}
	
	//window.clearTimeout(timeout);
	
	// testing
	//$j("#kayak-header").html("sub display: " + sub.css('display'));
}

function hideSub() 
{
	//var sub = $j(this).find("ul.submenu");
	//sub.hide();

	//window.clearTimeout(timeout);
	//timeout = window.setTimeout(showCurrent,3000);
}


function showTer() 
{
	// make sure termenu is hidden
	$j('ul.termenu').hide();
	
	var ter = $j(this).parent().find("ul.termenu");
	if(ter.css('display') == "none") { ter.fadeIn(); }
	
	$j(this).parent().parent().addClass("add-border");
	
	//window.clearTimeout(timeout);
	
	// testing
	//$j("#kayak-header").html("ter display: " + ter.css('display'));
}

function hideTer() 
{
	//var ter = $j(this).parent().find("ul.termenu");
	//ter.hide();
	
	//window.clearTimeout(timeout);
	//timeout = window.setTimeout(showCurrent,3000);
}

function showCurrent() {
	
	$j("ul.submenu").hide();
	$j("ul.termenu").hide();
	
	var sub = $j('li.current ul.submenu');
	sub.show();
	
	var ter = sub.find("a.active").parent();
	ter.find("ul.termenu").show();
	
	if(ter.find("ul.termenu").length == 1) sub.addClass("add-border");
	 
	 window.clearTimeout(timeout);
}

timeout = window.setTimeout(showCurrent,1000);



/*var desc = null;

function makeTall() {

	//window.clearTimeout(timeout);
	
	//if(desc != null) desc.hide();

	desc = $j(this).find("ul.submenu");
	
	desc.delay(200).fadeIn("fast");
	
	//setTimeout(createDelay,200);
	
	//$j('#active-menu').hide();
	
	if(desc.length != 0) {

	// 1. hide the active menu
	$j('#active-menu').hide();
	
	$j('#new-menu').show();
	
	// 2. remove any old collection-menu
	$j('.collection-menu').remove();
	
	// 3. add new menu
	
	
	
		$j("#new-menu").append('<ul class="collection-menu">' + desc.html() + '</ul>');
	}
	else {
		$j('#new-menu').hide();
	}
	//alert('<ul class="collection-menu">' + desc.html() + '</ul>');
}

function createDelay() {
	window.clearTimeout(timeout);
}

function makeShort() {

	desc = $j(this).find("ul.submenu");
	
	desc.fadeOut("fast");
	
	
	//window.clearTimeout(timeout);
	
	//timeout = window.setTimeout(closeMenu,500);
	
	//var desc = $j(this).find("ul.submenu");
	//desc.hide();
}

function closeMenu()
{
	// 1. hide the active menu
	//$j('#new-menu').hide();
	
	desc.hide();
	
	 $j('li.current ul.submenu').show();
	 $j('li.current li.current ul.termenu').show();
	 
	 //alert("lenght: " + $j('li.current li.current ul.termenu').length)
	
	 //
	
	// 1. hide the active menu
	//$j('#active-menu').fadeIn();
}*/

function showArrow() {
	$j(this).addClass("over-arrow");
}

function removeArrow() {
	$j(this).removeClass("over-arrow");
}

function setFeaturedInfo(id) 
{
	//hide all
	$j("#item1").hide();
	$j("#item2").hide();
	$j("#item3").hide();
	
	//show current
	$j("#" + id).fadeIn("slow");
	
	//remove all indicator classes
	$j("#indicator").removeClass("item1-ind");
	$j("#indicator").removeClass("item2-ind");
	$j("#indicator").removeClass("item3-ind");
	
	//add current indicator class
	$j("#indicator").addClass(id + "-ind");
}

var iviewer;

function createIframe(img)
{
	iviewer = document.createElement('IFRAME');

	iviewer.src = '/wp-content/themes/feathercraft/iviewer/viewer.html?'+ img;
	iviewer.id = "iviewerFrame";
	
	// make style object
	var obj = iviewer.style;
	
	// set container width/height
	obj.width = "100%";
	obj.height = "100%";
	
	obj.backgroundImage = "url(/wp-content/themes/feathercraft/images/80-white-back.png)";
	
	// position the container
	obj.position = "fixed";
	obj.zIndex = "999";
	
	obj.top = '0';
	obj.left = '0';
	
	/*if(options.position != "relative") {
	
		// deal with vertical alignment
		if(options.alignVert.margin == "center") {
			obj.top = '50%';
			obj.marginTop = ((options.width/2)*-1) + "px";
		} else {
			if(options.alignVert.margin == "top") obj.top = (options.alignVert.amount +'px');
			if(options.alignVert.margin == "bottom") { obj.bottom = (options.alignVert.amount +'px'); }
		}
	
		// deal with horizontal alignment
		if(options.alignHor.margin == "center") {
			obj.left = '50%';
			obj.marginLeft = ((options.width/2)*-1) + "px";
		} else {
			if(options.alignHor.margin == "left") obj.left = (options.alignHor.amount +'px');
			if(options.alignHor.margin == "right") obj.right = (options.alignHor.amount +'px');
		}
	}*/
	
	// set default iframe properties
	iviewer.setAttribute('marginWidth', '0');
	iviewer.setAttribute('marginHeight', '0');
	iviewer.setAttribute('vspace', '0');
	iviewer.setAttribute('hspace', '0');
	iviewer.setAttribute('frameBorder', '0');
	iviewer.setAttribute('scrolling', 'no');
	//iviewer.setAttribute('allowTransparency', 'true');

	iviewer.style.border = "0";
	

	document.body.appendChild(iviewer);
	
	
	$j('body').append("<div id='close-viewer' style='position: fixed;top: 1em;left: 1em;z-index: 9999;'><a href='javascript:removeViewer();' style='display:block;margin:10px 0 0 11px;border-bottom: 1px dotted #999;'><img src='/wp-content/themes/feathercraft/iviewer/img/zoom_close.png' alt='' width='180' height='37'></a></div>");
}

function removeViewer() {
	document.body.removeChild(iviewer);
	document.body.removeChild(document.getElementById('close-viewer'));
}


/*

function showBorder() {
	var img = $j(this).find("div.product-image");
	var desc = $j(this).find("div.product-details");

	img.addClass("product-image-over");
	desc.addClass("product-details-over");
}

function removeBorder() {
	var img = $j(this).find("div.product-image");
	var desc = $j(this).find("div.product-details");
	
	img.removeClass("product-image-over");
	desc.removeClass("product-details-over");
}


// filter dropdown
function dropOpen() {
	var desc = $j(this).find("ul.filter-options");
	desc.slideDown('fast');
}

function dropClose() {
	var desc = $j(this).find("ul.filter-options");
	desc.slideUp('fast');
}

function moveCloseLink(){ 
    var cb=document.getElementById('sb-nav-close'); 
    var tb=document.getElementById('sb-title'); 
    if(tb) tb.appendChild(cb); 
} 

*/
