/* Author: Andrew Henderson
 * Twitter: http://twitter.com/andrewhenderson
 * Email: anderw.m.henderson@gmail.com
 */

$(document).ready(function() {
	// global arrows
    rtArrow = '→';
    ltArrow = '←';
    dnArrow = '↓';
    upArrow = '↑';

    // IE conditional
    if($.browser.msie) {
      // IE specific behavior
    }
    else {
      // behavior for other browsers
    } // eo ie conditional

    // log in section
    $('header .login').click(function(){
        if($(this).hasClass('active')){
            $(this).toggleClass('active').blur();
            $(this).children('span').text(rtArrow);
            $('.login-section').hide();
            return false;
        }
        else {
            $(this).toggleClass('active').blur();
            $('.login span').text(dnArrow);
            $('.login-section').show();
            return false;
        }
    });
	// #main login link 
	$('#main .login').click(function() {
		$('html, body').animate({ scrollTop: 0 }, 'slow');
		setTimeout("$('header .login').click()", 200);
		$(this).blur();
		return false;
	});
	
	// password reset notice:
	if (/\/reset/.test(window.location)) alert("Your account's password has been reset and sent to the email you specified.\n\nIf you don't see the email within a few minutes, check your spam folder just in case.");

    // call nivo slider on home page
    if($('#hero').hasClass('nivo')) {
        $('html').css('overflow-x','hidden');
        $(window).load(function() {
            $('#slider').nivoSlider({
                effect:'boxRain', // Specify sets like: 'fold,fade,sliceDown'
                slices:15, // For slice animations
                boxCols: 8, // For box animations
                boxRows: 4, // For box animations
                animSpeed:700, // Slide transition speed
                pauseTime:7500, // How long each slide will show
                startSlide:0, // Set starting Slide (0 index)
                directionNav:false, // Next & Prev navigation
                directionNavHide:false, // Only show on hover
                controlNav:false, // 1,2,3... navigation
                controlNavThumbs:false, // Use thumbnails for Control Nav
                controlNavThumbsFromRel:false, // Use image rel for thumbs
                controlNavThumbsSearch: '.jpg', // Replace this with...
                controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
                keyboardNav:true, // Use left & right arrows
                pauseOnHover:false, // Stop animation while hovering
                manualAdvance:false, // Force manual transitions
                captionOpacity:1.0, // Universal caption opacity
                prevText: 'Prev', // Prev directionNav text
                nextText: 'Next', // Next directionNav text
                beforeChange: function(){}, // Triggers before a slide transition
                afterChange: function(){}, // Triggers after a slide transition
                slideshowEnd: function(){}, // Triggers after all slides have been shown
                lastSlide: function(){}, // Triggers when last slide is shown
                afterLoad: function(){} // Triggers when slider has loaded
            }); // eo nivo
        }); // eo window load
    } // if home
    
    // show the login form if hash is present (will be used instead of a "must log in" error)
    if($('#hero').hasClass('nivo'))	{	// is home
    	if (window.location.hash == '#login'){
    		$('header .login').click();
    	}
    }

    // make variable height divs equal heights
    $.fn.sameHeights = function() {
        $(this).each(function(){
            var tallest = 0;
            $(this).children().each(function(i){
                if (tallest < $(this).height()) { tallest = $(this).height(); }
            });
            $(this).children().css({'height': tallest});
        });
        return this;
    };

    // make boxes same height
    $('.cols.sameHeights').sameHeights();

    // hide show item info
    $('.item .info').click(function() {
        if($(this).hasClass('active')) {
            $(this).toggleClass('active').blur();
            $(this).parent().siblings('.more-info').slideUp();
        }
        else {
            $(this).toggleClass('active').blur();
            $(this).parent().siblings('.more-info').slideDown();
        }
        return false;
    });

    // show checkout button
    if($('header .cart').hasClass('full')) {
        $('header .checkout').show();
    }
    
	// cart item removal
	$('.cart-item-info .remove').click(function() {
		$(this).parent('.cart-item-info').parent('.tag').parent('.item').fadeOut('slow');
	});
    // clear login-error on appropriate actions
    $('.login-section input').click(function(){
    	$('#login-error').text('');	// clear any login-errors
    	$('#forgot-password-error').text('');	// clear any forgot-password-errors
    });
    $('.login-section #login-form-holder input').keypress(function(e){
    	if (e.which == 13){
    		$('#login-form').submit();
    	} else {
    		$('#login-error').text('');	// clear any login-errors
    	}
    });
    $('.login-section #forgot-password-form-holder input').keypress(function(e){
    	if (e.which == 13){
    		//$('#forgot-password-form').submit();
    	} else {
    		$('#forgot-password-error').text('');	// clear any forgot-password-errors
    	}
    });

	// load drop downs function
	dropdowns();
	
	// function to handle state fields (dropdown if usa/canada, free textbox otherwise)
	stateFieldsAlternate();
	
	// mock table additional styling
	$('div.table1 .tr').addClass('clearfix');
	
	$('.rate').rating({
		callback: function(value){
			$(this).click();
		}
	});

	// lightbox for screenshots:
	if ($('a[rel="lightbox-p"]').length > 0){	// make sure exists (so we know if we're on the page where lightBox is loaded)
		$('a[rel="lightbox-p"]').lightBox({fixedNavigation:true});
		$('a[rel="lightbox-s"]').lightBox({fixedNavigation:true});
	}
	
}); // eo document ready

// purchase button toggler
var toggleCheckoutButton = function(el){
	if ($(el).hasClass('remove')){
		$(el).removeClass('remove').children('input').first().attr('title', 'Add to Cart');
	} else {
		$(el).addClass('remove').children('input').first().attr('title', 'Remove from Cart');
	}
};

// update header cart info
var updateCart = function(items, price){
	if (items > 0){
		if (!$('header .cart').hasClass('full')) $('header .cart').addClass('full');
		$('header .checkout').show();
	} else {
		$('header .cart').removeClass('full');
		$('header .checkout').hide();
	}
	$('.items-view').text(items);
	$('.items-view-verbose').text(items + ' Product' + (items!=1?'s':''));
	$('.price-view').text(price);
	if (items < 1) $('#continue-checkout').attr('value', ltArrow+" Back to Store");
}

// glow effect ON - used for text fields or whatever
$.fn.glowOn = function() {
	$(this).blur().delay(1000).css({
		'border':'1px solid #15b4e5',
        '-moz-box-shadow':'0 0 8px rgba(82, 168, 236, 0.5)',
        '-webkit-box-shadow':'0 0 8px rgba(82, 168, 236, 0.5)',
        'box-shadow':'0 0 8px rgba(82, 168, 236, 0.5)',
        'outline':'none !important',
        '-webkit-transition-duration':'0.2s, 0.2s',
        '-webkit-transition-timing-function':'cubic-bezier(0, 0, 1, 1), cubic-bezier(0, 0, 1, 1)'
	});
}
// glow effect OFF - used for text fields or whatever
$.fn.glowOff = function() {
	$(this).blur().css({
	'border':'auto',
    '-moz-box-shadow':'0 0 0',
    '-webkit-box-shadow':'0 0 0',
    'box-shadow':'0 0 0',
    'outline':'none !important',
    '-webkit-transition-duration':'0.2s, 0.2s',
    '-webkit-transition-timing-function':'cubic-bezier(0, 0, 1, 1), cubic-bezier(0, 0, 1, 1)'
	});
}


/* custom drop down page script
Custom Images Date Selector Drop Down w/ jQuery
Author: John Lomma
Twitter: http://twitter.com/lommaj
Description: This code is part of dropDown.js. This code can be used in conjunction with that script
as either an external file or on the page level.
NOTE: This javascript is flexible and can be changed by altering the array 'm'. You may 
You may want to place this code on the page level rather than externally for a
greater level of customization.
------------------------------------*/

function dropdowns () {
	// read DD's from the global var the form field adds to (workaround in server-side DD class because DD/$ aren't loaded yet)
	if (window['FF_DD_N'] != undefined){
		$(FF_DD_N).each(function(index, dd){
			var bla = CreateDropDown($('#'+dd[0]), dd[1], dd[2], dd[3], dd[4], $('#'+dd[0]+'-hidden'));
			// and init their values:
			$('#'+dd[0]+'-hidden').val($('#'+dd[0]).children('.data').attr('rel'));
		});
	}	
}
function stateFieldsAlternate () {
	// to handle the list of states vs a free textbox for international province, etc.
	$('#field_state ul li').click(function(){
		if ($(this).attr('rel') == 'Other'){
			$('#field_state').hide();
			$('#field_state_free').val('');
			$('#field_state_free').show();
			$('#state-back').show();
		}
	});
	if ($('#field_state ul li.highlight').attr('rel') == 'Other'){	// page-load check:
		$('#field_state').hide();
		//$('#field_state_free').val('');	// might have a default
		$('#field_state_free').show();
		$('#state-back').show();
	}
	$('#state-back').click(function(){
		$(this).hide();
		$('#field_state_free').val('');
		$('#field_state_free').hide();
		$('#field_state').show();
		$('#field_state ul li[rel="0"]').click();
	});
}


// old star rating maybe to be fixed later:
/*
// star ratings
function ratings() {
	// hover
	$('.rating .star').hover(function() {
		$(this).addClass('hover');
		$(this).prev('.star').addClass('hover');
		$(this).prev('.star').prev('.star').addClass('hover');
		$(this).prev('.star').prev('.star').prev('.star').addClass('hover');
		$(this).prev('.star').prev('.star').prev('.star').prev('.star').addClass('hover');
		$(this).next('.star').removeClass('hover active');
		$(this).next('.star').next('.star').removeClass('hover active');
		$(this).next('.star').next('.star').next('.star').removeClass('hover active');
		$(this).next('.star').next('.star').next('.star').next('.star').removeClass('hover active');
	},
	//hover out
	function() {
		$(this).removeClass('hover');
		$(this).prev('.star').removeClass('hover');
		$(this).prev('.star').prev('.star').removeClass('hover');
		$(this).prev('.star').prev('.star').prev('.star').removeClass('hover');
		$(this).prev('.star').prev('.star').prev('.star').prev('.star').removeClass('hover');
		
		// re-establish selected
		// if the hovered is the currently selected rating, click it again
		if($(this).hasClass('selected')) {
			$(this).click();
		}
		// if the selected is another, click that one
		else {
			$(this).siblings('.star').each(function(){
				if ($(this).hasClass('selected')) {
					$(this).click();
				}
			});
		}
	});
	// click
	$('.rating input').click(function() {
		var rating = $(this).attr("value")
		var ratingID = "#" + $(this).attr("name")
		$(this).siblings().removeClass('selected');
		$(this).addClass('active selected');
		$(ratingID).val(rating);
		$(this).prev().addClass('active');
		$(this).prev().prev().addClass('active');
		$(this).prev().prev().prev().addClass('active');
		$(this).prev().prev().prev().prev().addClass('active');
		$(this).next().removeClass('active');
		$(this).next().next().removeClass('active');
		$(this).next().next().next().removeClass('active');
		$(this).next().next().next().next().removeClass('active');
		$(this).blur();
	});
	
	
}
*/
