// JavaScript Document

var MILTONS = {};
var cur_testimony_btn = 1;


$(function()
{
	// THESE FUNCTIONS TO LOAD WITH PAGE
	
	jQuery.MILTONS.navigation();
	//jQuery.MILTONS.navigation_cancel();
	jQuery.MILTONS.featured_specials_nav();
	jQuery.MILTONS.compactor('#specialsCategories','.categoryBtn','.subCategories');
	jQuery.MILTONS.delete_alert();
	jQuery.MILTONS.home_flash();
	jQuery.MILTONS.products_flash();
	jQuery.MILTONS.link_target('.xLink', '_blank');
	//jQuery.MILTONS.multi_tabs('#peopleAreTalkingNav', '.testimony', '.testimonyBtn');
	
	jQuery.MILTONS.people_are_talking_nav();
	
	
});



jQuery.MILTONS = {

	navigation : function() {
		
		$('#navigation').find('li').children('ul').css({display: 'none'});
		$('#navigation').find('li').hover(
			function(){
				//$(this).css({background: 'transparent url(/images/navigation/navigation_sprite.png) repeat-x 0 0', color: '#000000'});
				//$(this).children('a').not('.select').css({background: 'transparent url(/images/nav/nav_bkgd.jpg) repeat-x 0 -26px;', color: '#000'});
				$(this).children('ul').css({display: 'block'});
			},
			function() {
				//$(this).css({background: 'transparent url(/images/navigation/navigation_sprite.png) repeat-x 0 -36px', color: '#000000'});
				//$(this).children('a').not('.select').css({background: 'transparent', color: '#FFF'});
				$(this).children('ul').css({display: 'none'});
			}
		);
		$('#upperNav>ul>li:first').addClass('navEdgeL');
		$('#upperNav>ul>li:last').addClass('navEdgeR');
		$('#navigation>ul>li:first').addClass('navEdgeL');
		$('#navigation>ul>li:last').addClass('navEdgeR');
	},
	
	navigation_cancel : function () {
		$('#navigation').find('li').children('ul').css({display: 'none'});
		$('#navigation').find('a').css({cursor: 'default'}).click (function () {return false});
		//$('#upperNav').find('a').css({cursor: 'default'}).click (function () {return false});
		$('#upperNav').find('input').css({cursor: 'default'}).click (function () {return false});
		
		$('#upperNav>ul>li:first').addClass('navEdgeL');
		$('#upperNav>ul>li:last').addClass('navEdgeR');
		$('#navigation>ul>li:first').addClass('navEdgeL');
		$('#navigation>ul>li:last').addClass('navEdgeR');
		
		
	},
	
	multi_tabs : function(linkList, items, btnClass) {
		if (!btnClass) btnClass = '';
		
		var holder = $(linkList);
		if (holder.length > 0) {
			
			$(items).css({display: 'none'});
			$(linkList).find('a'+btnClass).click(
				function() { 
					var href = $(this).attr('href');
					
					$(linkList).find('a'+btnClass).not(this).removeClass('select');
					$(this).addClass('select');
					
					$('div'+items+':visible').not(href).css({display: 'none'});
					$(href).css({display: 'block'});
					
					return false;
				}
			);
			
			if (location.hash.length > 0){
				var tempLink = $(location.hash+'_btn');
				
				if (tempLink.length > 0) {
					$(tempLink).click();
				} else {
					$(linkList).find('a'+btnClass+':first').click();
				}
			} else {
				
				$(linkList).find('a'+btnClass+':first').click();
			}
		}
	},
	
	compactor : function ( list, btnLink, targetItem ) {
		$(list).find(targetItem).css({display: "none"});
		
		$(list).find(btnLink).click(
			function () {						
				var target = $(this).next(targetItem+':visible');
				var target_open = target.length;
				
				if (target_open > 0) {
					$(list).find(targetItem+':visible').hide(100);	
				} else {
					$(list).find(targetItem+':visible').hide(100);	
					$(this).next(targetItem).show(100);
				}
				return false;
			}
		);
		
		var startBtn = $(list).find(btnLink+'.select');
		if (btnLink.length > 0) {
			$(startBtn).click();
		} else {
			$(list).find(btnLink+':first').click();
		}
		
		
	},
	
	link_target : function (ID, target) {
		$(document).find(ID).each(
			function (){
				$(this).click(
					function (){
						var xLink = $(this).attr('href');
						window.open(xLink, target);
						return false;
					}
				);
			}
		);		
	},
	
	// CREATE A NEW METHOD AND PROPERTIES
 	modal_hijack_link : function( wrapper, trigger) 
	{
		// START AT THE TRIGGER <div> GET ALL THE CHILD <a> TAGS AND BUILD AN ARRAY TO HIGHJACK 
		$(wrapper).find('a'+trigger)
		
		// BY PASS ALL .delete BUTTONS TO PREVENT HIJACKLINK
		.not('.delete')
		
		// PREPARE FOR THE ONCLICK EVENT
		.click(function() {  
		
			// BUILD WIDTH AND HEIGHT FOR MODAL
			var W = $(window).width();
			var WH = $(window).height();
			var H = $('html').height();
			
			// HELPER FOR MODAL TO WORK ON SHORT PAGES
			if(H < WH){ 
				H = WH;
			}

			// INJECT THE MODAL INTO THE BODY TAG
			$("body").append('<div id="modal"></div>'); 
			
			// STYLE THE MODAL 
			$("#modal").css({display: "block", width: W, height: H, opacity: 0.7});		 

			// CREATE THE AHAH WINDOW TO EDIT THE RESULTS IN
			$("body").append('<div id="modalBox"></div>'); 
			
			// GET THE LINK TO HIJACK AS A STRING
			var urlJack = $(this).attr('href'); 
			var modalTitle = $(this).attr('rel');
			// STYLE THE AHAH WINDOW AND TRIGGER THE CANEL HIJACK
			var L = ($(window).width() - 630 ) / 2; // CENTER THE MODAL WINDOW IN THE VIEWPORT
			
			var modal = '<div id="modalTop">';
			if (modalTitle) modal += '<h1>'+modalTitle+'</h1>';
			modal += '<a id="cancelModal">Cancel</a>';
			modal += '</div><iframe src="" />';
			
			$("#modalBox").css({left: L}).append(modal);
			$('#modalBox iframe').attr({
				src: urlJack,
				width: 600,
				height: 600,
				frameborder: 0
			});
			
			jQuery.HOLTS.modal_cancel();
							
			// FOR IE6
			//$('html, body').animate({scrollTop:0}, 'slow'); 

		// DISABLE THE <a> TAG CUZ WE'RE HIJACKIN' IT
		return false; 
		
		});
		
	},
	
 	// CREATE A NEW METHOD AND PROPERTIES
	modal_cancel : function ()
	{
		$('#cancelModal').click(function(){
			$("#modalBox").remove();	
			$("#modal").css({display: 'none'});
		});
	},
		
	rotator_init : function(wrapper, container, image_array, speed)
	{
		var holder = $(wrapper).find(container);
		var array_length = image_array.length;
		
		// determine if container truely exists on page
		if (array_length > 0 && holder.length > 0){
		
			// interat through array and build images
			for (i=0; i < array_length; ++i) {
				$(holder).append('<img class="rotate" src="'+image_array[i]+'">');
			}
		
			// define starting image
			$(holder).find('img').not('img:first').css({display: 'none'});
			
			var img_start = $(holder).find('img:first');
			
			// begin rotation loop
			jQuery.MILTONS.rotate(img_start, speed);
		}
	},
	
	rotate : function (image, speed)
	{
		// set time out for rotation loop
		var t = setTimeout(
			function () {
				var next_image = $(image).next();
				
				// fade image
				$(image).fadeOut(750,
					function () {
						// determine if there is a next image. if not return to first to loop to
						if (next_image.length > 0) {
							jQuery.MILTONS.rotate(next_image, speed);
						} else {
							next_image = $(image).parent().find('img:first');
							jQuery.MILTONS.rotate(next_image, speed);
						}
					}
				);
				
				// determine if there is a next image. if not return to first to fade in
				if (next_image.length > 0) {
					$(next_image).fadeIn(750);
				} else {
					$(image).parent().find('img:first').fadeIn(750);							
				}
				
			}, speed // loop based on speed in miliseconds
		);
	},
	
	delete_alert : function()
	{
		$('.delete').click(function(){
			return confirm('Are you sure you want to delete this? It can not be undone.'); // OLD SCHOOL :)
		});
	},
	
	featured_specials_nav : function() {
		$('#featuredSpecialsNav>ul>li:first').addClass('navEdgeL');
		$('#featuredSpecialsNav>ul>li:last').addClass('navEdgeR');
	
		var btns = $('#featuredSpecialsNav a'); 
		
		if (btns.length > 1) {
			jQuery.MILTONS.multi_tabs('#featuredSpecialsNav', '.specialsItem');
		} else {
			$('#featuredSpecialsNav').hide();
		}
		
	},
	
	products_flash : function()
	{
		var holder = $('#productsFlashContent');
		
		if (holder.length > 0) {
			var boys_check = $('body.boys');
			var logoGlow = (boys_check.length > 0) ? false : true;
		
			var productXML_href = $(holder).find('a.productXML').attr('href');
			var infoXML_href = $(holder).find('a.infoXML').attr('href');
			var rel = $(holder).find('a.productXML').attr('rel');
			
			var flashvars = 'productXML='+productXML_href+'&infoXML='+infoXML_href+'&curImage='+rel;
			if (logoGlow === true) flashvars+='&logoGlow='+logoGlow;
			
			$.post("/utilities/flash", { src:'/flash/productComponent.swf', width:750, height:575, flashvars: flashvars },
				function(data){
					$(holder).html(data);
					$(holder).css({display: "block"});
				}
			);
		
		}
	},
	
	home_flash : function()
	{
		var holder = $('#homeFlashContent');
		
		if (holder.length > 0) {
			
			$.post("/utilities/flash", { src:'/flash/homeComponent.swf', width:610, height:290, scale: 'exactfit', salign: 'T' },
				function(data){
					$(holder).html(data);
					$(holder).css({display: "block"});
				}
			);
		
		}
	},
	
	people_are_talking_nav : function ()
	{
		var holder = $('body.peopleAreTalking');
		
		$('.testimony').hide();
		$('.testimonyGroup').not('.testimonyGroup:first').hide();
		
		if (holder.length > 0) {
			var testimonials = $('#peopleAreTalkingNav a.testimonyBtn');
			
			$(testimonials).click(
				function () {
					cur_testimony_btn = $(this).attr('rel');	
					
					var testimonial = $(this).attr('href'); 
					
					$('#peopleAreTalkingNav a.select').removeClass('select');
					$(this).addClass('select');
					
					$('.testimony').hide();
					$(testimonial).show();
					
					$('.testimonyGroup').hide();
					$(this).parent().parent().show();
					
					return false;
				}
			);			
			
			var testimonyGroups = $('#peopleAreTalkingNav li.groupBtn a');
			
			$(testimonyGroups).click(
				function () {
					var testimonialGroup = $(this).attr('href'); 
					
					if ($(this).hasClass('prevGroup')) {
						$(testimonialGroup).find('a.testimonyBtn:last').click();
					} else {
						$(testimonialGroup).find('a.testimonyBtn:first').click();
					}
					
					$('.testimonyGroup').hide();
					$(testimonialGroup).show();
					
					return false;	
				}
			);
		
			$('a.next').click(
				function () {
					if (cur_testimony_btn < testimonials.length) {
						cur_testimony_btn++;
					} else {
						cur_testimony_btn = 1;	
					}
					var btn = 'testimonial_'+cur_testimony_btn+'_btn';
					$('#'+btn).click();
					return false;
				}
			);
			
			$('a.prev').click(
				function () {
					if (cur_testimony_btn > 1) {
						cur_testimony_btn--;
					} else {
						cur_testimony_btn = testimonials.length;	
					}
					var btn = 'testimonial_'+cur_testimony_btn+'_btn';
					$('#'+btn).click();
					return false;
				}
			);
			
			if (location.hash.length > 0){
				var tempLink = $(location.hash+'_btn');
				
				if (tempLink.length > 0) {
					$(tempLink).click();
				} else {
					$('#peopleAreTalkingNav').find('a.testimonyBtn:first').click();
				}
			} else {
				
				$('#peopleAreTalkingNav').find('a.testimonyBtn:first').click();
			}
		
		}
		
	}
	
	
	
};










