(function($) {
  jQuery.fn.backgroundPosition = function() {
    var p = $(this).css("background-position");
    if(typeof(p) === "undefined") return $(this).css("background-position-x") + " " + $(this).css("background-position-y");
    else return p;
  };
})(jQuery);

(function($) {
  $.fn.easyOverlay = function(){	
    $(this).click(function(e){
      if($("#easyOverlay")) { $("#easyOverlay").remove(); }
      $("body").append("<p id='easyOverlay'><a href='javascript:void(0)' class='closeit'>X</a></p>");
      $("<img src='"+ this.rel +"' alt='' />").appendTo('#easyOverlay').load(function() {
        var wide = ($(window).width() / 2) - ($(this).width() / 2);
        var high = ($(window).height() / 2) - ($(this).height() / 2);			
        $("#easyOverlay").css({
          top: high + "px",
          left: wide + "px",
		  		display: 'none',
          visibility: "visible"
        }).fadeIn();
      });
    });
    $("a.closeit").live("click", function(){
      $("#easyOverlay").fadeOut();
    });
  };
})(jQuery)

function inc(filename)
{
	var hbody = document.getElementsByTagName('head')[0];
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	hbody.appendChild(script)
}

function topSearch()
{
	if ( $(".top-search-field").css("display") == "none" )
	{
		$('.top-search-field').show();
		$('.bold-chat').css('opacity', 0.2);
	} 
	else
	{
		var searchvalue = $('.top-search-field input').val();
		if ( searchvalue != '' && searchvalue != 'Enter search here...' ) {
			$('.top-search-field').closest('form').submit();
		}
		else {
			$('.top-search-field').hide();
			$('.bold-chat').css('opacity', 1);
		}
	}
}

function bindSearch()
{
	$("input[name=type]").bind("click", function(e){
																										
		var par = $(this).parent();
		var pos = par.backgroundPosition().split(" ");
		
		if (pos[0] == "-179px")
		{
			$(".p-reds-w label").each(function(e){
				$(this).removeAttr("style");
			});
			par.css("background-position", "-179px " + pos[1]);
		}
		else
		{
			par.css("background-position", "0px " + pos[1]);
		}
		
		$(".year").hide();
		
	});
}

function bindSearch2()
{
	var year	= '<option value="">Year</option>';
	var date	= new Date();
	var yearNow = date.getFullYear() + 2;
	for (i = yearNow; i >= 1960; i--)
	{
		year += '<option value="'+i+'">'+i+'</option>';
	}
	
	$("#s-year").html(year);
	
	$("#s-year").change(function(e){
		
		$("#s-make").load('rs_ajax.php?act=get_makes');
		$(".make").hide();
		
	});
	
	$("#s-make").change(function(e){
		
		$("#s-mode").load('rs_ajax.php?act=get_model&id=' + $(this).val());
		$(".mode").hide();
		
	});

	$("#s-mode").change(function(e){
		var redirect_url = $("#s-make").val() + '/' + $("#s-mode").val() + '/' + $("input[name=type]:checked").val() + '.html?year=' + $("#s-year").val();
		//$(this).closest("form").submit();
		//window.location = 'model.php?cPath=' + $("#s-make").val() + '_' + $("#s-mode").val() + '&type=' + $("input[name=type]:checked").val() + '&year=' + $("#s-year").val();
		window.location = redirect_url;
	});
}

function bindParttype()
{
	$(".parttype a").bind("click", function(e){

		var par 	= $(this).parent();
		var cls		= $(this).attr("class");
			
		if ( par.attr("class") == "ina" ) {
			$(".parttype div").each(function(e){
				$(this).attr("class", "ina");
			});
			par.attr("class", "act");
		}
		else {
			par.attr("class", "ina");
		}
		
		showProducts(cls);
		
	});
}

function bindCart()
{
	$(".t-c-btn a").bind("click", function(e){
																			 
		var pos = $(".nav-03").backgroundPosition().split(" ");
		
		if ( pos[1] == "-65px" ) {
			$(".nav-03").css("background-position", "0px 0px");
			$(".pull-cart").slideDown('normal');
		} else {
			$(".pull-cart").slideUp('normal', function(e){
				$(".nav-03").css("background-position", "0px -65px");
			});
		}
		
	});
}

function showParttype(cls)
{
	if ( $(".t_" + cls).css("display") == "none" )
	{
		$(".products").hide();
		$(".t_" + cls).show();
	}
	else
	{
		$(".t_" + cls).hide();
	}
}

function showAnswer(cls)
{
	if ( $("." + cls).css("display") == "none" )
	{
		$(".answer").hide();
		$("." + cls).show();
	}
	else
	{
		$("." + cls).hide();
	}
	toggleScroll('#' + cls);
}

function showMakes(cls)
{
	if ( $(".m_" + cls).css("display") == "none" )
	{
		$(".makes").hide();
		$(".m_" + cls).show();
		toggleScroll(".index-makes");
	}
	else
	{
		$(".m_" + cls).hide();
	}
}

function showProducts(id)
{
	if ( $("#" + id).css("display") == "none" )
	{
		$(".list-items").hide();
		$("#" + id).show();
	}
	else
	{
		$("#" + id).hide();
	}
}

function showProductDetail(id)
{
	if ( $("#" + id).css("display") == "none" )
	{
		$(".product-info").hide();
		$("#" + id).show();
		toggleScroll("." + id);
	}
	else
	{
		$("#" + id).hide();
	}
}

function addQty(fieldClass)
{
	var value = $('.' + fieldClass).val();	
	value = parseInt(value);
	
	$('.' + fieldClass).val(value + 1);
}

function minQty(fieldClass)
{
	var value = $('.' + fieldClass).val();
	value = parseInt(value);
	
	if (value != 0)
		$('.' + fieldClass).val(value - 1);
}

function toggleScroll(idClass)
{
	$.scrollTo(idClass, 1000);
}

function setScroll(hash)
{
	if (hash != '')
	{
		model = null;
		makes	= hash.replace('#', '');
		
		createCookie('rsCookie', makes, 1);
		
		if ( makes.indexOf('|') > 0 ) {
			model = makes.split('|');
			
			makes	= model[0];
			model = model[1];
		}
		
		if (model != null) {
			var winPtype = $('.' + model).parent();
			winPtype.attr("class", "act");
			showProducts(model);
		}
		
		showParttype(makes);
		toggleScroll('.m_' + makes);
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function update_comments(text)
{
	$('#comments_paypal').val(text);
	$('#comments_cc').val(text);	
}

function trackingOpen()
{
	$('.tracking-window-wrap').fadeIn();
	$('#head').fadeTo('normal', 0.25);
	$('#body').fadeTo('normal', 0.25);
	$('.logo-wrap').fadeTo('normal', 0.25);
}

function trackingClose()
{
	$('.tracking-window-wrap').fadeOut();
	$('#head').removeAttr('style');
	$('#body').fadeTo('normal', 1);
	$('.logo-wrap').fadeTo('normal', 1);
}

var clickedIt = false;
function cleartextarea(id){
	if (clickedIt == false){
		id.value="";
		clickedIt=true;
	}
}

function check_tracking()
{
	if ($('#email_track').val() != '' && $('#zip_track').val() != '')
	{
		return true;
	}
	else
	{
		return false;	
	}
}

function resize(maxNum) {
  
	$(".img-thumb").each(function(e){
		
		if ($(this).width() > $(this).height()) {
			if ($(this).width() > maxNum)
				$(this).width(maxNum);
		}
		else {
			if ($(this).height() > maxNum)
				$(this).height(maxNum);
		}
		
	});
}

function is_int(value)
{
	if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))){
  	return true;
 	} 
	else {
  	return false;
 	} 
}

$(function(){
	bindCart();
	/* detecting any makes */
	makes = location.hash;
	makes	= makes.replace('#', '');
	
	if ( is_int(makes) ) {
		showProductDetail('p_' + makes);	
	}
	else {
		$.get('rs_ajax.php?act=decrypt&code=' + makes, function(data) {
			showProductDetail('p_' + data);																				
		});
	}
	
	
	/*
	if ( makes.indexOf('_') > 0 && makes.indexOf('|') == -1 ) {
		makes	= makes.replace('#', '');
		makes = makes.replace('_', '');
		switchCategory('cat_' + makes);
	} else {
	}
	*/
		
	$.preloadCssImages();
	$(".screenshot").easyOverlay();
});