//////////////////////////////////////////////////////////////////////////////////
// Overlabel
//////////////////////////////////////////////////////////////////////////////////
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = $(this); var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = $(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.not('.readonly').css("display", "none");
            }
            control.focus(function () {label.not('.readonly').css("display", "none");}).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = $(this); var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};
//////////////////////////////////////////////////////////////////////////////////
// Document Ready
//////////////////////////////////////////////////////////////////////////////////
$(document).ready(function(){


  // external links
	$('a[rel=external]').click(function(){
		window.open(this.href);
		return false;   
	});
	
	
	// Sign up Form
	$('#signup label, #contactForm label, .commentsForm label').overlabel();
	$('input.text').focus(function(){
		$(this).parents('p.input').addClass('focus');
	});
	$('input.text').blur(function(){
		$(this).parents('p.input').removeClass('focus');
	});
	
	// Clickable Blocks
	$('#shareMeatbun li').click(function(){
		window.location=$(this).find("a").attr("href"); return false;
	});
	
	// Form Validation
	if (jQuery.fn.validate){
		$('#contactForm form, #registerForm form, #checkoutForm form, .commentsForm form').validate();
		$('#signup button').click(function(){

		  $(this).parents('form').validate();
		});
	}
	// Featured Posts
	var $featured = $('#featuredPosts div'),
			featuredCycleOpts = {
				timeout: 0,
				slideExpr: 'div',
				prev: '.prev',
				next: '.next'
		  };
	if ($featured.length > 1) {
		$('#featuredPosts').append('<ul class="slideNav"><li class="prev"><a>Previous</a></li><li class="next"><a>Next</a></li></ul>').cycle(featuredCycleOpts);
	}
	
	// Opacity Hovers
	$('#relatedPosts li, .addPics li, .addColors li, #featuredPosts .bg').hover(function(){
		$(this).find('img').stop().fadeTo("fast", 0.75);
	}, function(){
		$(this).find('img').stop().fadeTo("fast", 1);
	}); 
	
	// Equal Columns
/*
	var mainY = $('#primaryContent').height(),
		sidebarY = $('#secondaryContent').height();
	if (mainY > sidebarY + 400) {
		$('#secondaryContent').css({'height': mainY, 'padding-bottom' : 0});
	} */

	
	// Item Hovers
	$('.itemList li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	// Size Select Validate
	var $select = $('#addCartForm select, #registerForm select, #checkoutForm select'),
			$form = $select.parents('form');
	$select.change(function(){
		$(this).siblings('.error').remove();
	});
	$form.submit(function(){
		if($select.val() == '') {
			if ($(this).siblings('.error').length) {
				return false;
			} else {
				$select.after('<label class="error">&larr; Please Select an Option.</label>');
			}
			return false;
		}
	});


/*
		*/

	//  Item Pics

	var $itemPics = $('.itemPics .slide img'),
			itemCycleOpts = {
				timeout: 0,
				slideExpr: 'img',
				prev: '.prev',
				next: '.next',
				pager:  '.addPics ul',
				pagerAnchorBuilder: function(idx, slide) { 
					return '.addPics ul li:eq(' + idx + ') a';
				}
		  };
	if ($itemPics.length > 1) {
		$('.itemPics .slide').append('<ul class="slideNav"><li class="prev"><a>Previous</a></li><li class="next"><a>Next</a></li></ul>').cycle(itemCycleOpts);
	}
	
	
	// Same As

  $('.sameAs input').click(function() {
    
    if ($(this).is(":checked")) {
          $('#shippingInfo input:not(#sameAs)', ':visible', document.body).each(function(i) { 
            $(this).val( $('#billingInfo input').eq(i).val() );
            //fetch select values
            var billCountry = $("#billCountry").val();
            var billState = $("#billState").val();
            //set select values
            $("#shipCountry").val(billCountry);
            $("#shipState").val(billState);  

          });
        } else {
          $('#shippingInfo input:not(#sameAs)', ':visible', document.body).each(function(i) { 
            $(this).val("");
            $("#shipCountry, #shipState").val('none');

          });
        }
                  
  });     

	
	if(jQuery.fn.fancybox){
  	$('a.box').fancybox({
			overlayOpacity : .85,
			padding : 20,
			frameWidth : 400,
			frameHeight : 580,
			hideOnContentClick : false 
		});
	}
	
	
	
	// CVV Popup
/*
	$.setupJMPopups({
		screenLockerBackground: "#fff",
		screenLockerOpacity: "0.7"
	});
	$('a.box').click(function(){
		var url = $(this).attr('href');
		$.openPopupLayer({
			url: url
		});   
		return false; 
	});*/

	
	
});
