//////////////////////////////////////////
//  Delay Plugin
/////////////////////////////////////////
(function($){
	$.fn.delay = function(time, callback){
		jQuery.fx.step.delay = function(){};
		return this.animate({delay:1}, time, callback);
	}
})(jQuery);
////////////////////////////////////////
// Over Label
////////////////////////////////////////
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.css("display", "none");
            }
            control.focus(function () {label.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();
                }
            });
        }
    });
};
//////////////////////////////////////////
// Tool Tip
/////////////////////////////////////////
(function($){
	$.fn.tooltip = function(options) {
		var defaults = {  
			contentToShow: '.pic'  
		};  
		var options = $.extend(defaults, options);
		
		// var loaderPath = '/media/images/loaderSmall.gif';

		this.each(function() {

			var $this = $(this);
			var $url = $this.attr('href');

                
				var $content = $this.find(options.contentToShow);
	
				// Remove default attribute behaviour
				this.title = '';

				$this.hover(function(e){
					// Make sure previously displayed tip is out of DOM
					$('.tt').remove();
					// Create Tool Tip
					$('<div class="tt" />').appendTo('body').hide().html($content)
					// Position tool tip and fade it in
					.css({ top: e.pageY - 210, left: e.pageX + 20})
					// Fade it in
					.delay(100).fadeIn(100);

				}, function(e){
					$('.tt').remove();
				});

				// Mouse Move
				$this.mousemove(function(e) {
					$('.tt').css({
						top: e.pageY - 130,
						left: e.pageX - 265
				     });
				});
		  });
		  return this;
	}
})(jQuery);

$(document).ready(function(){
	// Home Slides
	if(jQuery.fn.cycle) {
		$('#homePage div.slide').delay(2500, function(){
			$(this).cycle({ 
				speed : 1000, 
				timeout : 2500 
			});
		});
	}
	// fancy corners
	$('#homePage #feature, .pagePic.square').append('<span class="crnr nw" /><span class="crnr ne" /><span class="crnr se" /><span class="crnr sw" />')
	// Head Bounce
	$('#primaryNav li a').mousedown(function(){
		$('#primeBranding h1 a').css('background-position', '50% -400px');
	});
	$('#primaryNav li a').mouseup(function(){
		$('#primeBranding h1 a').css('background-position', '50% 0');
	});
	// Carousel
	$('.carousel').each(function(){ 
		var $items = $('.carousel ul');
		var carouselNav = '<ul class="carouselControls"><li class="prev"><a>Previous</a></li><li class="next"><a>next</a></li></ul>';
		if ($items.length > 1) {
			$(this).after(carouselNav);
			$(this).cycle({ 
				fx:     'scrollHorz', 
				speed:  800, 
				timeout: 0,
				prev: '.prev a',
				next: '.next a'
			});
		}  
	});
	$('#theLatest .carousel').each(function(){ 
		var $items = $('.carousel li');
		var carouselNav = '<ul class="carouselControls"><li class="prev"><a>Previous</a></li><li class="next"><a>next</a></li></ul>';
		if ($items.length > 1) {
			$(this).parents('#theLatest').append(carouselNav);
			$(this).cycle({ 
				fx:     'scrollHorz', 
				speed:  800, 
				timeout: 0,
				prev: '.prev a',
				next: '.next a'
			});
		}  
	});
	// Events Scroll
	$('a.startEvent').click(function(){
		if (jQuery.fn.scrollTo) {
		   $(window).scrollTo('#eventInquiries', 800); 
		}
	});  
	
	// Tooltip
	$('#menu li').tooltip();
	// item detail slides
	$('#itemSlide .slide').cycle({
		fx:     'fade', 
		speed:  500, 
		timeout: 0, 
		next: '.slide img', 
		pager:  'ul.thumbs',
		pagerAnchorBuilder: function(idx, slide) { 
			return 'ul.thumbs li:eq(' + idx + ') a'; 
		}   
	});
	// Contact Form
	$('#eventForm label, #comForm label, #contactForm label').overlabel();
	if(jQuery.fn.validate) {
		$('#eventForm, #contactForm, #comForm').validate(); 
	}  

	$("a.fb").fancybox({
		'frameWidth':800,
		'frameHeight':600,
		'callbackOnShow':setSize
	}); 
	
	function setSize()
	{		
		fbX = $('.press-box').width();
		fbY = $('#fancy_content').height();
		foX = $('#fancy_outer').width();
		diff = foX-fbX;
		$('#fancy_outer').css('width', fbX+40);
		$('#fancy_outer').css('height', fbY+40);
		$('#fancy_outer').css('margin-left', diff/2);
	}
	
	//----------------SUBMIT FORM  
	function submitform(n)
	{  
	  document.forms[n].submit();
	}
	
	//////////////////////////////////////////
	//  Product Size Select
	/////////////////////////////////////////
	$('div.sized').each(function(){
		$this = $(this);
		var id = '#'+$(this).attr('id');
		
		$(id+' .size-select').change(function(){					
			$value = $(this).attr('value');
			$name = $(id+' .item-title').html();
			$(id+' .item-name').attr('value', $name + ' ' + $value);
		});
		
		$(id+' .qty').change(function(){
			$value = $(this).attr('value');
			$(id+' .quantity').attr('value', $value)
		});
		
		$(id+' .submit').click(function(){
			if($(id+' .size-select').val() == 'none'){
				$(id+' .error').show();
				return false;
			}	
		});
	});
	
	$('div.unsized').each(function(){
		$this = $(this);
		var id = '#'+$(this).attr('id');	
		
		$(id+' .qty').change(function(){
			$value = $(this).attr('value');
			$(id+' .quantity').attr('value', $value)
		});		
	});
	
	$('a[rel*=external]').click(function(){
		window.open(this.href);
		return false;   
	}); 	

});

//----------------SUBMIT FORM  
function submitform(n)
{  
  document.forms[n].submit();
}
