var current_tab;
var tabs;

$(document).ready(function(){
		
	$('.titles_link_design').click(function(){;
		$(this).colorbox({height: '630px', width: '300px', inline:true, href: $(this).attr('box')});	
	});
		
		//$.fn.colorbox({width:"50%", inline:true, href:"#welcome_popup"});
	
	if($('#welcome_popup').exists()){
		$('body').prepend('<div id="overlay"></div>');
		$('#welcome_popup').show();
	}
	
	$('#welcome_close').click(function(){
		$(this).parent('div').hide();
		$('#overlay').hide();
	});
		
	/** Contact Page **/
	$('#contact_reset').click(function(){
		$('#contact_form').clearForm();
	});
	
	$('.phone_piece').blur(function() {
		var phone_number = $('#p1').val() + '' + $('#p2').val() + '' + $('#p3').val();
		$('#contact_number').val(phone_number);		
	});
	
	/** Cart Colorbox Link **/
	$(".cart_link").colorbox({href:'/cart_popup.cgi', height: "60%"});
	
	$(".cart_ship_link").colorbox({html: '<p>Calculating shipping options... one moment please.</p><div style=\'width:100%;text-align:center;padding-top:30px;\'><img src=\'../images/loading.gif\'></div>',width: '300px', height:'300px'});
	//$(".cart_preview_link").colorbox({html: '<div style=\'width:100%;text-align:center;padding-top:30px;\'><img src=\'../images/loading.gif\'></div>',width: '300px', height:'300px', href: 'http://espn.go.com' });
	//$('.cart_preview_link').click(function(){;
	//	$(this).colorbox({height: '630px', width: '300px', inline:true, href: $(this).attr('box')});	
	//});
	//$(".cart_preview_link").colorbox({href:'', height: "60%"});
	$('.cart_preview_link').colorbox({height: '650px', width: '500px'});
	
	$('.view_larger').colorbox({height: '500px', width: '500px'});
	$('.comp_chart_link').colorbox({height: '630px', width: '720px'});
	
	$('.titles_link').colorbox({height: '630px', width: '300px'});
	
	/** Top Nav Rollovers (Each has its own because of weird gradiants, also I wanted to try to get away without cutting rollover background images) **/
	$(".top_nav_home").hover(
		function(){
			$(this).css("background-color", "#506ca2");
		},
		function(){
			$(this).css("background-color", "transparent");
		}
	);	
	
	$(".top_nav_products").hover(
		function(){
			$(this).css("background-color", "#597eb4");
		},
		function(){
			$(this).css("background-color", "transparent");
		}
	);	
	
	$(".top_nav_design").hover(
		function(){
			$(this).css("background-color", "#659bcf");
		},
		function(){
			$(this).css("background-color", "transparent");
		}
	);	
	
	$(".top_nav_resource").hover(
		function(){
			$(this).css("background-color", "#6dafe3");
		},
		function(){
			$(this).css("background-color", "transparent");
		}
	);	
	
	$(".top_nav_faq").hover(
		function(){
			$(this).css("background-color", "#6aa6db");
		},
		function(){
			$(this).css("background-color", "transparent");
		}
	);	
	
	$(".top_nav_about").hover(
		function(){
			$(this).css("background-color", "#6499ce");
		},
		function(){
			$(this).css("background-color", "transparent");
		}
	);
	
	/** FAQ Color Box Controls **/
	$(".faq_link").colorbox({width:"50%", inline:true, href: function(){var href = "#" + $(this).parents('.question').next().attr("id"); return href;}});
	
	//$('#coloroption').click(function(){
		
	/** Color Chart Links **/
	$('.color_chart_link').click(function(){
		//Get the ID of this color which is stored in a custom colorID tag
		var id = $(this).children().attr('colorID');

		
		//select that color in the select drop down
		var selector = "#coloroption option[value='" + id + "']";
		$(selector).attr('selected', 'selected');
					
		$('#color_chooser_popup').slideUp('slow');
					
		return false;
	});
		
	$('.color_popup_opener').click(function(){
		$('#color_chooser_popup').slideDown('slow');
		return false;
	});
		

	/** Calculate Sash Add to Cart **/
	/**	$('#sash_add_to_cart').click(function(){
	  var re1='(\\[)';	// Any Single Character 1
      var re2='(\\+)';	// Any Single Character 2
      var re3='( )';	// White Space 1
      var re4='(\\$[0-9]+(?:\\.[0-9][0-9])?)(?![\\d])';	// Dollar Amount 1
      var re5='(\\])';	// Any Single Character 3

      var p = new RegExp(re1+re2+re3+re4+re5,["i"]);
		
		// Get sash info and place in current sashes div
		var sashTitle = $('#sashtitle').html();
		var sashRibbonColor = $('#coloroption :selected').text().replace(p, '');
		var sashPrintColor = $('#printoption :selected').text().replace(p, '');;
		var sashRosette = $('#rosetteoption :selected').text();
		var sashAmount = $('#quantity').val();
		
		//If the didnt even select a sash before hitting add to cart
		if(sashRibbonColor == 'Sash Color' || sashPrintColor == 'Print Color')
			return false;
		
		if(sashRosette != 'Add Rosette [+ $2.00]'){
			sashRosette = '';
		}else{
			sashRosette = 'Rosette';
		}
		var sashString = '<p style="font-family: Georgia; font-size: .8em;">' + sashTitle + '<br/> &nbsp;&nbsp; ' + '[' + sashRibbonColor + '/' + sashPrintColor + '/' + sashRosette+ ']' + '<span style="float: right; font-weight: bold;">Quantity: ' + sashAmount + '</span></p>';
		
		$('#currentSash_wrapper').append(sashString);

	});**/
	
	$('#sash_add_to_cart').click(function(){
		
			$.ajax({
				url: "/cart_sashes_ajax.cgi",
				type: "POST",
				data: '',
				cache: false,
				contentType: 'application/x-www-form-urlencoded',
				dataType: "text",
				success: function(msg){
					$('#cart_sashs_ajax_wrapper').html(msg);
				}
			});
		
		$(this).parents('form').clearForm();
			
	});
	
	var undefined;
	var turnon = $('#sash_turnon').attr('number');
	
	if(turnon != undefined){
		$('#titleoption').val(turnon);
		
		$('#sash_choose_message').hide();
		$('#sash_hider').show();
		// Hide all the images and view larger links
		$('.sash_title_image').hide();
		$('.sash_title_link').hide();
		
		// show ones appropriate for this sah
		$('#title_image_' + turnon).show();
		$('#larger_image_' +turnon).show();
		
		$('.stock_subcategory').each(function(){$(this).removeClass('stock_active');});
		$('#tab-' + turnon).addClass('stock_active');
	}
	
	
	/** This is the functionality for stock products title tabs **/
	tabs = $('.block_tab');
	current_tab = 0;
	
	//we need to keep the current tab placement incase they clicked a different title tab and then afterwards click view more titles
	$('.stock_subcategory').click(function(){
		var i=0;
		var clicked = $(this).attr('id');
		
		$('.stock_subcategory').each(function(){
			if($(this).attr('id') == clicked)
				current_tab = i;
			i++;
		});
	});
	
	$('.view_more_link').click(function(){
		
		var this_link = $(this);	
		var i=0;	
		
		$('.block_tab').each(function(){
			
			if(this_link.parents('.block_tab').attr('id') == $(this).attr('id')){
				current_tab = i;
			}			
			
			$(this).css('display', 'none');
			
			i++;
		});
				
		if(current_tab + 1 > tabs.length-1){current_tab = 0;}
		else{current_tab++;}
		
		try{
			$('.block_tab').eq(current_tab).css('display', 'block');
			$('.stock_subcategory').each(function(){
				$(this).removeClass('stock_active');
			});
			$('.stock_subcategory').eq(current_tab).addClass('stock_active');
			
		}catch(err){
			alert(err);
		}		
		
		var elementClicked = $(this);
		var destination = $(elementClicked).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: (0-destination)}, 500 );

		return false;
	});
	/** End Stock Product Title Tabs Functionality **/
	
	/** Open a stock item to the correct title tab **/
	var tabhash = location.hash;
	
	if(tabhash !== '' && $('#stock_title_area').exists() && tabhash !== '#'){
		var pieces = location.href.split("/");
		if(pieces[4] == 'sashes')
			setTimeout('switchtabs_sash()', 500);
		else
			setTimeout('switchtabs()', 500);
	}
	
	
	/** Removes last line separator from stock product title list **/
	$('.block_tab').each(function(){
		$('.title_sep:last', this).remove();
	});
});

/***
* Used on stock items to select the block which contains the title which is denoted by the hash tag in the URL. 
* Afterwards its sets the on button to the correct tab
*/
function switchtabs() {
	var tabhash = location.hash;
	tabhash = tabhash.substr(1);
	
	var foundin = $('span:contains("' + tabhash + '")');
	
	$('.block_tab').each(function(){$(this).css('display', 'none');});
	foundin.parents('.block_tab').css('display', 'block');

	
	var parentid = foundin.parents('.block_tab').attr('id');
	
	var count = 0;
	$('.block_tab').each(function(){
		
		if($(this).attr('id') == parentid){
			current_tab = count;
		}
		count++;
	});
	
	$('.stock_subcategory').each(function(){
		$(this).removeClass('stock_active');
	});
	$('.stock_subcategory').eq(current_tab).addClass('stock_active');
}

/***
* Used on sashs to select a tab based on the location hash
*/
function switchtabs_sash() {
	var tabhash = location.hash;
	tabhash = tabhash.substr(1);
	
	var foundin = $('.stock_subcategory a:contains("' + tabhash + '")');
	
	var parentid = '#' + foundin.parent().attr('id')
	$(parentid).addClass('stock_active');
	$(parentid).trigger('click');

}

/** 
*Estimate Quick Ribbon Price Functions
**/

function product_graphics() {

	var image_path = "images/";

	document.getElementById('product_or1').src = image_path + "product_or.gif";
	eval("document.getElementById('product_or2').src = image_path + \"product_or.gif\";");
}

function estimateQuick(rid, endline, url, sub) {


		var linkname = "/estimate_popup_quick.cgi?rid=" + rid + "&endline=" + endline + "&url=" + url + "&subid=" + sub;

		
		$(".estimate_link_quick").colorbox({
			href:linkname, 
			height: "63%",
			width:"40%",
			iframe: true
		});

}

function estimateCustom(rid, endline, url, sub) {


		var linkname = "/estimate_popup_custom.cgi?rid=" + rid + "&endline=" + endline + "&url=" + url + "&subid=" + sub;

		
		$(".estimate_link_custom").colorbox({
			href:linkname, 
			height: "63%",
			width:"40%",
			iframe: true
		});

}

function selectRibbon(url) {

	//$.colorbox.close();
	document.location = url;

}



$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = 0;
  });
};

jQuery.fn.exists = function(){return jQuery(this).length>0;}
