// JavaScript Document

//MODAL WINDOWS
$(function() { $('a.nyroModal').nyroModal(); });

$(window).load(function(){

	//Vertically align images in product slideshow
	$('#productimages img').vAlign();	
	
	//Vertically align images in slider
	$('.slideproduct img').vAlign();		

});

$(document).ready(function(){

	//SEARCH FORM
	var searchDefault = 'Search Olsen Chain & Cable...';
	if( $('#searchq').val() == '' ){
		$('#searchq').val( searchDefault );
	}
	//Hide default text
	$('#searchq').keypress(function(){
		if( $(this).val() == searchDefault ){
			$(this).val('');
		}
	});
	//Show default text
	$('#searchq').focusout(function(){
		if( $.trim( $(this).val() ) == '' ){ 
			$(this).val( searchDefault );
		}
	});

	//HERO AREA	
	$("#slider").easySlider({
		pause: 5000,
		auto: true, 
		continuous: true,
		numeric: true,
		numericId: 'slider_controls'
	});
	
	//PRODUCT IMAGES SLIDER
	if($('#productimages').length > 0){
		
		//Initialize slider
		$("#productimages").easySlider({
			auto: false, 
			continuous: true,
			nextId: "productimages_next",
			prevId: "productimages_prev"
		});	
		
		$('#productslidezoom a').click(function(e){
			e.preventDefault();
			//Get the URL of the large photo from the current slide
			var largePhotoURL = $('li.onstage a.nyroModal').attr('href');
			console.log(largePhotoURL);
			$(this).attr('href', largePhotoURL);
			$(this).nmCall();
		});
	}

	
	//OVER STATES for HOMEPAGE BANNERS, etc.
	$('.hoverfade, .itemphoto a').hover(
		function(){ $(this).fadeTo(100, 0.8); },
		function(){ $(this).fadeTo(400, 1);}
	);
	
	//SLIDER FOR BOTTOM (PRODUCTS) and ITEM OVER STATES
	if($('#products_slider').length > 0){

		//PRODUCTS SLIDER
		$("#products_slider").easySlider({
			auto: false, 
			continuous: true,
			nextId: "products_next",
			prevId: "products_prev"
		});	
	
		//PRODUCTS MOUSEOVER -- injects a 'screen' div with product title and button
		$('.slide_product').hover(
			function(){
				var prodLink = $(this).children('a').attr('href');
				var prodTitle = $(this).children('a').children('img').attr('title');
				//Create screen div
				var screenDiv = $('<div class="product_screen"></div>');
				screenDiv.css('cursor','pointer');
				screenDiv.append('<h3>' + prodTitle + '</h3>');
				screenDiv.append('<div class="redbutton_short noarrow"><a href="' + prodLink + '">VIEW <span class="doublearrow">&#9658;&#9658;</span></a></div>');
				screenDiv.click(function(){
					window.location = prodLink;
				});
				//Dim product image
				$(this).children('a').fadeTo(170, 0.13);
				$(this).append(screenDiv);				
			},
			function(){
				//Remove screen div
				$(this).children('.product_screen').remove();
				$(this).children('a').fadeTo(600, 1);
			}
		);

	}
	
	//TABBED CONTENT
	if($('.tabcontent').length > 0){
		$('.tabcontent .tab').hide();
		$('.tabcontent .tabnav li:first').addClass('on');
		$('.tabcontent .tab:first').show();
		$('.tabcontent .tabnav a').click(function(){
			//Get ID of tab to open
			var openTab = $(this).attr('rel');
			//Close all/any open tabs
			$(this).parents('.tabcontent').find('.tab').hide('fast');
			$(this).parents('.tabcontent').find('li.on').removeClass('on');
			//Open the new tab
			$(this).parent().addClass('on');
			$(this).parents('.tabcontent').find('#' + openTab).show('fast');
			return false;
		});	
	}
	
	//BREADCRUMBS
	$('#breadcrumbs a.storebc:last').css('fontWeight', 'bold');
	
	//Vertically align images
	$('.brandbadge img').vAlign();
	
	//SHOPPING CART PAGES
	$('.submitlink').show();
	$('.hideform').hide();
	
	//Preload loading indicator
	var loadIcon = $('<img src="images/ajax-loader.gif" title="Loading" alt="Loading" style="padding:6px; background-color:#FFF">');

	//Hides buttons after click -- prevents clicking again
	if($('.hideclick').length > 0){
		$('.hideclick').click(function(){
			$(this).hide('fast');
			loadIcon.insertAfter($(this));
		});	
	}
	
});	


//Plugins, etc.
(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = Math.ceil((ph-ah) / 2);
	//console.log(ah + ' : ' + ph + ' : ' + mh );
	$(this).css('margin-top', mh);
	});
};
})(jQuery);
