/*
	Wagner Skis scripts (utilizing jQuery 1.2.6)
	Developed by Saforian
*/

/* --- Initialize page --- */
$(document).ready(function(){

	// Stripe row colors
	$("table.data tr:nth-child(even)").not(".even").not(".odd").addClass("even");

	// Insert document icons
	//$("a[href$=pdf]:not(:has(img))").append('<img class="icon" src="../images/icon_pdf.gif" width="14" height="14" alt=" (PDF)">');
	$("a[href$=doc]:not(:has(img)), a[href$=docx]:not(:has(img))").append('<img class="icon" src="/images/icon_word.gif" width="14" height="14" alt=" (Word Document)">');
	$("a[href$=xls]:not(:has(img)), a[href$=xlsx]:not(:has(img))").append('<img class="icon" src="/images/icon_excel.gif" width="14" height="14" alt=" (Excel Spreadsheet)">');
	$("a[href$=ppt]:not(:has(img)), a[href$=pptx]:not(:has(img))").append('<img class="icon" src="/images/icon_powerpoint.gif" width="14" height="14" alt=" (Powerpoint Presentation)">');

	// Column childs CSS3 fix
	$(".columns .col:last-child").addClass("last-child");
	$(".columns .col:first-child").addClass("first-child");
	$(".columns.three .col:eq(1)").addClass("middle-child");

	// IE6 helper functions
	if ($.browser.msie && $.browser.version < 7) {

		// PNG fix
		iepngfix();
	}

	
	$("#utility-navigation li:last-child").addClass("first");
	$('#subnav ul li:last-child').addClass("last");
	

	// Add bottom background image to the page wrapper
	$('#wrapper').after('<div id="wrapbtm"></div>');
	$('.bigquote').wrap('<div class="beginquote"><div class="endquote"></div></div>');



	/* Homepage cycle box */
	$('#profilewrap')
		.after('<div id="cyclenumbers">')
		.cycle({
			fx: 'fade',
			speed: '900',
			timeout: 5000,
			pause: true,
			pager: '#cyclenumbers'
		});

	$("div#cyclenumbers").append('<div class="white"></div>');



/*
zENCART sECTION
*/

$('.boxn')
.wrapInner('<div class="inner"></div>')
.append('<div class="boxnbtm"></div>');

$('.boxl')
.wrapInner('<div class="inner"></div>')
.append('<div class="dblwideformbtm"></div>');


/*
~~~~~
*/

	$("#home #shallwe, #home #signup, #searchsubmit, #addtocart").hover(function(){
		$(this).addClass(("hover"));
	}, function(){
		$(this).removeClass(("hover"));
	});



	$("#storecategories .storecat").hover(function(){
		$(this).children('p').fadeIn();
	}, function(){
		$(this).children('p').fadeOut();
	});


	/* Flickr Hover */
	$("div.flickr_badge_image a").hover(function(){

		var $flickImage = $('#flickr-main');
		var $newImage = $(this).children().attr('src').split("_s.jpg")[0];
		var $flickLink = $('#flickr-photo a');
		var $newTitle = $(this).children().attr('title');
		var $newLink = $(this).attr('href');

		// change the image source, link and link title..:
		$flickImage.attr({src : $newImage + ".jpg"});
		$flickLink.attr({href : $newLink});
		$flickLink.attr({title : $newTitle});
		$(this).addClass(("hover"));

	}, function(){

		$(this).removeClass(("hover"));

	});


	/* Navigation: Show active subnavigation link and mark wrapper to match page name */
	var urlpaths = location.href.split("?")[0].split("#")[0].split("/");

	if (urlpaths[4]) {
		// Match the path filename to a link
		$("#subnav ul li a[href*='" + urlpaths[4] + "']").addClass("active");

	} else {
		// Assume index page
		$("#subnav ul li a[href$='index.php']").addClass("active");
	}



	/* Ski of the week thumb switcher */
	$("div#skipictures .thumbs img").hover(function(){

		var $mainImage = $('#main-image');
		var $newImage = $(this).attr('src');
		var $newTitle = $(this).attr('title');
		// change the image source, title..:
		$mainImage.attr({src : $newImage});
		$mainImage.attr({title : $newTitle});
		$(this).addClass(("hover"));

	}, function () {

		$(this).removeClass(("hover"));

	});

	$('#skidetails #description .option').append('<img src="../images/arrow-optionadd.gif" alt="Add option" width="18" height="11" />');


	/* Skier Gallery: Colorbox overlays */
	$("#gallery a.overlay").each(function(){
		$(this).colorbox({width:"1080px", inline:true, href:$(this).attr("href")});
	});
	/* youtube overlay */
	$("a.youtube").each(function(){
		$(this).colorbox({width:"460px", height:"420px", inline:true, href:$(this).attr("href")});
	});

	/* Vimeo overlay */
	$("a.vimeo").each(function(){
		$(this).colorbox({width:"440px", height:"300px", inline:true, href:$(this).attr("href")});
	});



	/*colorbox class declared*/
	$("a.thickbox").colorbox();




 $("#newcustomersignupform-open").click(function () { 
		$('#loginsignup').fadeOut('fast');
		$('#newcustomersignupform').fadeIn('fast');
		return false;
    });

 $("#newcustomersignupform-close").click(function () { 
		$('#newcustomersignupform').fadeOut('fast');
		$('#loginsignup').fadeIn('fast');
		return false;
    });


/*wORD pRESS sECTION*/

$('li.cat-item a, ul#recentposts li a').prepend('> ');

/*
sKIER dNA fORM
*/

	//client-side validation for the first section (not very secure!)
	$("#skierdna input.required").blur(function() {
		var $thismessage = $(this).next('.error');
		var $required_length = $(this).val().length;
		//empty the error message
		$thismessage.empty();
		if ($required_length < 1)
		$(this).after(' <span class="error">*required</span>');
	});
	

	
	//previous and next buttons
	$('.nextpage a').click(function() {
		var $thispage = $(this).parent(0).parent('.formpage');
		var $nextpage = $(this).parent(0).parent('.formpage').next('.formpage');
		var $requiredfields = $('input.required');//client-side validation (not very secure!)
		var $required_length = $requiredfields.val().length;//client-side validation
		if ($required_length < 1)//client-side validation
		{
		$('.error').empty();//client-side validation
		$requiredfields.after(' <span class="error">*required</span>');//client-side validation (not very secure!)
		return false;//client-side validation
		}
		else
		{
		$thispage.slideToggle("slow");
		$nextpage.slideToggle("slow");
		}
	return false;
	});
	$('.previouspage a').click(function() {
		var $thispage = $(this).parent(0).parent('.formpage');
		var $previouspage = $(this).parent(0).parent('.formpage').prev('.formpage');
		$thispage.slideToggle("slow");
		$previouspage.slideToggle("slow");
	return false;
	});
	//~~~~~~~~~~
	//toggle the dark grey fields onfocus
	$('.name-email input, #current-ski input').focus(function() {
		$(this).toggleClass('normal');
		});
		$('.name-email input, #current-ski input').blur(function() {
		$(this).toggleClass('normal');
	});
	


	/*
      Form Field Value Swap. If they "unfocus" and it's empty, then it repopulates.
    */

swapValues = [];
    $(".swap_value").each(function(i){
        swapValues[i] = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function(){
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });

});



/* --- IE6 PNG fix --- */
function iepngfix() {

	// IE 5.5 and 6.0 PNG filter support (derived from youngpup.net)
	$("img[src$=png]").each(function(){
		var src = this.src;
		var div = document.createElement("div");

		// Set replacement div properties
		div.id = this.id;
		div.className = this.className;
		div.title = this.title || this.alt;
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizing='scale')";
		div.style.width = this.width + "px";
		div.style.height = this.height + "px";

		// Replace image with transparent div
		this.replaceNode(div);
	});
}