$(document).ready(function(){
	
	/* PNGFIX start *******************************************************************/
		$(document).pngFix();
	/* PNGFIX end *********************************************************************/
	
	/* EXTERNAL LINKS start ***********************************************************/
	$('a[@rel="external"]').click(function(){
		this.target = "_blank";
	});	
	/* EXTERNAL LINKS end *************************************************************/
	
	/* NAVIGATION start ***************************************************************/
	//$('#nav ul ul').prepend('<li class="top"></li>');
	//$('#nav ul ul').append('<li class="bottom"></li>');
	$('#nav ul ul').hide();
	//hover show next level
	$('#nav ul > li').hover(function() { $('ul:first', this).show(); },function() {$('ul:first', this).hide();});
	//add class for flyouts
	if ($('#nav ul ul ul').length > 0) { $('#nav ul ul ul').prev().addClass('flyout'); }
	
	$('#nav-sub li:first').addClass('first');
	$('#nav-sub li:last').addClass('last');
	/* NAVIGATION end ****************************************************************/
	
	/* SUB NAVIGATION start **********************************************************/						
						$('#nav-sub li.level2 ul').hide();
						
						$('#nav-sub li.level2').each(function(){
							//alert($(this).length);
							
							if($('ul',this).length > 0){
								$(this).addClass('subs');
								$(this).prepend('<span class="toggle" href="#"></span>');

								$('.toggle',this).bind("click",function(){
									$(this).next().next().toggle(function(){
										if($(this).is(':visible')){
											$(this).parent().removeClass('subs');
											$(this).parent().addClass('subs-on');
										}
										else{
											$(this).parent().removeClass('subs-on');
											$(this).parent().addClass('subs');
										}
									});
								});
							}
						});	
	/* SUB NAVIGATION end ************************************************************/
	
	
	
	/* SIFR***************************************************************************/
	sifr('.font-01','font-goudy-old-style',2.4);
	/* SIFR**************************************************************************/

	/* COMMERCE:img click replace****************************************************/	
	$('.prod-sub-img').click(function() { $('.prod-image').html($(this).html()); return false });
	/*COMMERCE:img click replace****************************************************/
	
	/* LIVE CHAT */
	$('#live-chat > a').attr({id: 'btn-live-chat'}).css('visibility','visible');
	$('#live-chat > a img').hide();
	/* LIVE CHAT */
	
	/* SLIDESHOW start **************************************************************/
	if($('#slideshow').length > 0){
		var slideshow_init = setInterval( "slideSwitch()", 5000 );
		$('#slideshow .thumbs a').click(function(){
			clearInterval(slideshow_init);
			$('#slideshow .display a').removeClass('active');
			$('#slideshow .display #'+$(this).attr('rel')).addClass('active');
		});
		
		$('#slideshow .thumbs').jScrollPane({showArrows:true,arrowSize:13}).css({margin:0,float:'none',display:'inline'});
	}
	/* SLIDESHOW end ****************************************************************/
	
	
	if($.jScrollPane){
		
		/*WHATS HAPPENING start**********************************************************/
		$('#heres-whats-happening ul').jScrollPane({showArrows:true,arrowSize:13});	
		/*WHATS HAPPENING end************************************************************/
		
		/*ACTIVITIES - EVENTS start******************************************************/		
		$('#tab-today dl').jScrollPane({showArrows:true,arrowSize:13});
		$('#tab-this-week dl').jScrollPane({showArrows:true,arrowSize:13}).parent('div').css('display','none');
		$('#tab-this-month dl').jScrollPane({showArrows:true,arrowSize:13}).parent('div').css('display','none');
		$('#tab-this-season dl').jScrollPane({showArrows:true,arrowSize:13}).parent('div').css('display','none');
		
		$('#activities-events ul li h3').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;').css('text-indent','0'); //ie stupidity
		$('#activities-events ul li h3').click(function(){
			$('#activities-events ul li').removeClass('active');
			$('#activities-events ul li div.jScrollPaneContainer').css('display','none'); //cuz of jscrollpane
			$(this).parent('li').addClass('active');
			$(this).next().css('display','block'); //cuz of jscrollpane
		});	
		/*ACTIVITIES - EVENTS end********************************************************/
	}
	
	/* MAP LIST */
	if($('#map-list').length > 0){
		$('#map-list').load('/includes/inc_map_list.php');
	}	
	/* MAP LIST */
	
	/* COMMERCE:dropdown click replace****************************************************/
	$("#switch-item").change(function () {
		var price = "";
		var image = "";
		$("#switch-item option:selected").each(function () {
					price += "Regular: " + price_array[$(this).val()]; //grab price array item
					//if there is a sale...
					if (sale_price_array[$(this).val()] && sale_price_array[$(this).val()] != price_array[$(this).val()]) price += " Sale: " + sale_price_array[$(this).val()] + " ";
					image += image_array[$(this).val()] + " "; //grab image array item
					image = image.replace(/ /g,''); //replace image whitespace
				});
		$(".prod-price").text(price);
		$("div.prod-image a").attr('href',image);
		$("div.prod-image img").attr('src',image);
	})
	.change();
	/*COMMERCE:dropdown click replace****************************************************/	
	
}); //close document.ready

/* SIFR function start **************************************************************/
function sifr(x,font,adj){
$(x).flash(
	{ src: '/includes/media/'+font+'.swf' },
	 { version: 8 },
		function(htmlOptions) {
			var charlength = (this.innerHTML.length) * $(this).height()/adj;
			htmlOptions.flashvars.css = '*%20%7B%20color%3A%23'+RGBstring($(this).css('color'))+'%7D';
			htmlOptions.flashvars.txt = this.innerHTML;
			this.innerHTML = '<span>'+this.innerHTML+'</span>';
			var $alt = $(this.firstChild);
			htmlOptions.width = charlength; //$alt.width()
			htmlOptions.height = $(this).height(); //$alt.height()
			$alt.addClass('sifr-alt');
			$(this).addClass('sifr-text-alt').prepend($.fn.flash.transform(htmlOptions));						
		}
	).css({visibility:'visible'});
}
/* SIFR function end ****************************************************************/


/* sIFR RGB conversion function start **********************************************/
function RGBstring(str) {
	str1 = str.replace("#","");
	str2 = str1.replace("rgb(","");
	str2 = str2.replace(")","");
	str2 = str2.split(",");
	if(navigator.appName == "Microsoft Internet Explorer"){ return str1; }
	else { return toHex(str2[0])+toHex(str2[1])+toHex(str2[2]); }
}
function toHex(N) {
 if (N==null) return "01";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}
/* sIFR RGB conversion function end *************************************************/

/* SLIDESHOW function start *********************************************************/
function slideSwitch() {
	var $active = $('#slideshow .display a.active');

	if ( $active.length == 0 ) $active = $('#slideshow .display a:last');

	var $next =  $active.next('a').length ? $active.next()
			: $('#slideshow .display a:first');

	$active.addClass('last-active');
	
	$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function() {
			 $active.removeClass('active last-active');
	});
	
}
/* SLIDESHOW function end ***********************************************************/


function addToAjaxCart(internalID){
	var error = false;
	$('#fProductDetail_'+internalID+' select').each(function(){
															 	//alert($(this).val());
		if($(this).val() == '0'){
			alert('Please select all required options');
			error = true;
		}
	});
	
	if(!error){
		if($('#fProductDetail_'+internalID+' select').length>0 && $('#fProductDetail_'+internalID+' select').val() == '0'){
			alert('Please select all required options');
			//$('#nPID_'+internalID).append('<p class="error-sm">Please select required options.</p>');
		} else {
			$('.output').fadeOut('').html('');
			$("#buttons-"+internalID).prepend('<img src="/commerce/includes/images/loading.gif" width="16" height="16" style="vertical-align:middle; border:0 !important;" />');
			$("#bSubmitButton_"+internalID).attr('disabled',true);
			
			$.post("/commerce/checkout/ajax-basket.cfm", $('#fProductDetail_'+internalID).serialize(),function(data,status) {
				$('.error-sm').hide();
				$("#output_"+internalID).fadeIn().html(data);
				$("#buttons-"+internalID+" img").remove();	
				$("#bSubmitButton_"+internalID).attr('disabled',false);
			});
		}
	}
	
	
	return false;
}


/*ADD TO CART*/
function addToCart(formID,internalID) {
	$('.output').fadeOut('').html('');
	$("#bSubmitButton_"+internalID).attr('disabled',true).html('').addClass('loading');

	$.post("/checkout/basket_includes.cfm", $('#'+formID).serialize(),function(data,status) {
		//$("#basket-contents").load("/includes/nav/basket-contents.cfm",{box_cart_show: 1});
		$("#bSubmitButton_"+internalID).attr('disabled',false).removeClass('loading').html('<span class="button"><span>Add To Cart</span></span>');
		$("#output_"+internalID).fadeIn().html('<p class="success-sm">Item was successfully added to your cart. <a class="button small" href="/checkout/basket.cfm"><span>View Cart</span></a></p>');
	});
	return false;
}
/*ADD TO CART*/

/* QUICK VIEW DETAILS */
function quickDetails(internalID){
	
	var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
	var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
	
	$('body').prepend('<div id="popup-bg"></div><div id="quick-details"></div>');

	//centering 
	$("#quick-details").css({ "top": getYX("Y")+20,	"left": (document.documentElement.clientWidth/2)-($("#quick-details").width()/2) });
	$("#popup-bg").css({ width:$('html').width(),height: getYX('Y'),opacity:0.5  });
	
	$("#quick-details").addClass('loading');
	$("#quick-details").load("/detail/view-quick-details.cfm?quickDetails=1&nPID="+internalID,function(){	
		$("#quick-details").removeClass('loading');
		$('#popup-bg').click(function(){ popUpClose(); });
		$('#quick-details .close').click(function(){ popUpClose(); });
	});
	$("#popup-bg").css({ width:$('html').width(),height: _docHeight,opacity:0.5  });
	return false;
	
}
/* QUICK VIEW DETAILS */

function popUpClose(){
	$('#popup-bg').fadeOut();
	$('#popup-bg').next().fadeOut().remove();
	return false;
}

function detailTab(displayBlock){
	$('.tab-block').removeClass('active');
	$('.tab').removeClass('active');
	$('#'+displayBlock+'-tab').addClass('active');
	$('#'+displayBlock).addClass('active');
	$('.tab-blocks').css({ "height": $('.tab-blocks .active').height()+50 }); //keep overflow visible
}



function galleryPopup(id,width,height){
	var image = $('#'+id).attr('src');
	var description = $('#'+id).attr('title');
	$('body').prepend('<div id="popup-bg"></div><div id="gallery-image"><a class="close" onclick="popUpClose();">close [X]</a><img src="'+image+'"><p>'+description+'</p></div>');

	$("#popup-bg").css({ width:$('html').width(),height: $('html').height(),opacity:0.5  });
	$("#gallery-image").css({ "top": getYX("Y")+40,	"left": (document.documentElement.clientWidth/2)-($("#gallery-image").width()/2) });
	$('#popup-bg').click(function(){ popUpClose(); });
	$('#gallery-image .close').click(function(){ popUpClose(); });
	return false;
}

function getYX(coord){
	var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
	if(coord=="X"){
		return scrOfX;
	}
	else if(coord=="Y"){
		return scrOfY;
	}
}

function popUpWindow(URLStr, scrollbar, resizable, left, top, width, height){
  var popUpWin=0;
  if(popUpWin) {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrollbar+',resizable='+resizable+',copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
