/**
 * PShelper.com
 * Base Store Version javascript
 * @author Derek Decker derek@visioncourse.com
 * @author Andrew Maxwell andrew@visioncourse.com
 * @author Bryant Young bryant@visioncourse.com
 *
 * Notes:
 *
 * When creating text box inputs, always apply both
 * a value and default attribute (the same value)
 *
 * For rollovers use attribute 'hoversrc' to define the
 * rollover image source
 *
 **/

	var trace = function(t){ if("console" in window)console.log(t); },
		buttonize = (function($value,$tag){ // Searches for inputs with $value, applies class with $tag
        if (!$tag) { $tag = $value.toLowerCase().replace(/ /g,''); } // "Add to Cart" becomes addtocart
    
        var target = $('input[value="' + $value + '"]');
        var value = $value;
        var tag = $tag;
        var class_prefix = 'custom_button custom_';
        
        // Special Cases based on 1st character of $value
        if ( $value.substring(0,1) == '.' || $value.substring(0,1) == '#') target = value;        // Target class or id
        if ( $value.substring(0,1) == '^') target = $('input[value^="' + value.substr(1) + '"]'); // Starts With
        if ( $value.substring(0,1) == '$') target = $('input[value$="' + value.substr(1) + '"]'); // Ends with
        if ( $value.substring(0,1) == '*') target = $('input[value*="' + value.substr(1) + '"]'); // Wildcard
    
        // Strip classes and value, apply custom button class
        $(target).val('&nbsp;').attr('class','').addClass(class_prefix + $tag);
    });
	
$(function(){
	//input textbox handling
    $('input[type="text"]').focus(function(){
        if($(this).val()==$(this).attr('alt'))
        {
            $(this).val('');
        }
    }).blur(function(){
        if($(this).val()=='')
        {
            $(this).val($(this).attr('alt'));
        }
    });
	
	
	var vertSpan = (function(){
		var offset = 232; //header height + footer height + top / bottom padding for body-right (40px)
		var divToSpan = $('#product-list');
		
		if(divToSpan.height() < ($(window).height() - offset)){ //if the content is smaller than the window minus offset
			var newHeight = $(window).height() - offset;
			if($.browser.msie && parseInt($.browser.version) < 8.0){
				newHeight += 40;
				$('#main').css({'overflow':'hidden'});
			}
			divToSpan.height(newHeight); //update it
		}
	});vertSpan();
	$(window).bind('resize',vertSpan);
	

    $('a[rel="external"]').attr('target','_blank');


    $('a[href="#"]').click(function(event){
	    event.preventDefault();
	    return false;
    });
	
	//rollover image handling
     var preload = [],
		 xOffset = 10,
		 yOffset = 20;

     $('img').each(function(){
        //preloader
        if($(this).attr('popsrc'))
        {
            preload.push($('<img src="'+$(this).attr('popsrc')+'"/>'));

            $(this).css({'cursor':'pointer'}).hover(
            //mouse over
            function(e){
                $("body").append("<div id='imagepop'><img src='"+$(this).attr('popsrc')+"'/></div>");
                $("#imagepop")
                    .css({"top":(e.pageY - xOffset) + "px","left":(e.pageX + yOffset) + "px",'border':'2px solid #000000','position':'absolute','z-index':'100'})
                    .show();
            },
            //mouse out
            function(){
                $("#imagepop").remove();
            })

            $(this).mousemove(function(e){
                $("#imagepop").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
            });
        }
     });
	
	buttonize('View');
	buttonize('Buy');
	buttonize('Add to Cart'); buttonize('Add To Cart');
	buttonize('Add to Wishlist'); buttonize('Add To Wishlist');  buttonize('Add to Wish List');
	buttonize('Save');
	buttonize('Sign-In','signin'); buttonize('Sign In','signin');
	buttonize('Log-In','login'); buttonize('Log In','login'); buttonize('Login','login');
	buttonize('Register');
	buttonize('Update');
	buttonize('Save Cart');
	buttonize('Proceed to Checkout >>', 'checkout');
	buttonize('Continue Shopping','continue');
	buttonize('Get Balance');
	buttonize('Join');
	buttonize('Search');
	buttonize('Send');
	buttonize('Clear');
	buttonize('Reset','clear');

	$('form[name=cartcheckoutpro] img').hide();
	$('.wish-list-icon').hide();
    
    Cufon.replace('#cat-header')('h1')('.sku')('.answer-head')('.question').set('fontFamily', 'CabinSketch').replace('#home-canvas p');
	
	$('img').each(function(){
	  this.draggable = false;
	  this.onmousedown = function(event) {
					event.preventDefault();
					return false;
		};
	  this.oncontextmenu = function(event) {
					event.preventDefault();
					return false;
		};
	})
});	

