jQuery(document).ready(function(){
    if(document.getElementById('locator')) {
		createLocator();
	}
	if(document.getElementById('homeslide')) {
		homeslide();
	}
	/*
	jQuery('a[href*=popup.aspx]').click(function(e) {
	    e.preventDefault();
	    var win = window.open(this.href,'emailSignup','height=560,width=560,location=no,menubar=no,scrollbars=yes,status=yes,titlebar=yes,toolbar=no');
	    win.focus();
	});
	*/
	
	// Anchor Hacks
     jQuery('a').each(function(index) {
        var href = jQuery(this).attr('href');
        
        if(-1 == href.toLowerCase().indexOf("://"))
        {
            if(href.substr(0,1) =='/')
            {
                href = 'http://' +window.location.hostname+href;
            }
            else
            {
                href = 'http://' +window.location.hostname+'/'+href;
            }
        }
        var hrefs = href.split(/\/+/g);
        
        //google analytics
        if(typeof pageTracker != 'null' && typeof pageTracker != 'undefined' && window.location.hostname!=hrefs[1])
        {
            var domains = ["store.tech4o.com","store.silvacompass.com","www.silvacompass.com","www.tech4o.com"];
            for ( var domain in domains )
            {
                if(-1 < hrefs[1].toLowerCase().indexOf(domains[domain]))
                {
                    jQuery(this).click(function(e){
	                    pageTracker._link(href);return false;
                    });
                }
            }
        }
        
        //popup.aspx
        if(-1 < href.toLowerCase().indexOf("/popup.aspx"))
        {
            jQuery(this).click(function(e){
                e.preventDefault();
                var win = window.open(this.href,'emailSignup','height=560,width=560,location=no,menubar=no,scrollbars=yes,status=yes,titlebar=yes,toolbar=no');
                win.focus();
            });
        }
    });
    
    // Temporary Javascript for Debugging
	if(window.location.hostname=="localhost") {
        jQuery('img').each(function(index) {
            var src = jQuery(this).attr('src');
            if(src.substr(0,1) =='/')
            {
                src = 'http://' + window.location.hostname + src;
            }
            jQuery(this).attr('src',src);
        });
    }
    
});

function createLocator() {
    Map = new DealerMap();
    //Map.baseIconURL = '/webimages/gold.gif';
    Map.brand = 11;
    Map.initialLocation = 'Oklahoma';
    Map.loadMap();
}

function submitAddress() {
    if(document.getElementById('locator')) {
		createLocator();
	}
	Map.showAddress(jQuery('#g_address').val(),jQuery('#g_distance').val());
}


// home flash
function HomeFlash(xmlFile){
	var swfName = "Slideshow";
	var swfFile = "/swf/Slideshow.swf";
	var swfWidth = 958;
	var swfHeight = 364;
	var swfWMode = "opaque";
	var swfFullscreen = "false";
	var swfMenu = "false";
	var swfBGColor = "#ffffff";
	var vars = "xmlFile="+ xmlFile;
	var html = loadFlash(swfName, swfFile, swfWidth, swfHeight, swfWMode, swfFullscreen, swfMenu, swfBGColor, vars);
	document.write(html);
}

function NewCaptchaCode(){
    jQuery.ajax({
        async: true,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/webservice.asmx/NewCaptchaCode",
        data: "{}",
        dataType: "json",
        success: function(result, textStatus) {
            jQuery('#imgCaptcha').attr("src", "/Captcha.aspx?"+result);
        },
        error: function(request, status, err) {
            alert('failure - ' + status + ' ' + err);
        },
        complete: function (XMLHttpRequest, textStatus) {
            return false;
        }
    });
}

function GoSearch(){
    var searchtext = escape(jQuery("#search .textbox").val());
    window.location = "/search.aspx?searchtext=" + searchtext;
    return false;
}

function GoSearchEnter(e){
    if(!e) e = window.event;
     var key = e.which;
     if(!key) key = e.keyCode;
     if (key && key == 13) {
        GoSearch();
        return false;
     }
     return true;
}

function homeslide() {
	var $homeslide = jQuery('#homeslide'),
		$slides = $homeslide.find('li.slide'),
		$content = $homeslide.find('div.content'),
		SlideshowDelay = parseFloat($homeslide.attr('data-SlideshowDelay')) * 1000,
		TransitionSpeed = parseFloat($homeslide.attr('data-TransitionSpeed')) * 1000,
		ProductFadeIn = parseFloat($homeslide.attr('data-ProductFadeIn')) * 1000,
		current = 0,
		currentNum = 0,
		timer,
		queue = -1,
		queueDirection = "left",
		transition = false;
		
	function stopTimer() {
		clearTimeout(timer);
		timer = null;
	}
	function setTimer() {
		timer = setTimeout(nextSlide, SlideshowDelay);
	}
	
	function finishAnimation(num) {
		$content.eq(current).hide();
		$content.eq(num).fadeIn(ProductFadeIn,function() {
			current = num;
			transition = false;
			if(queue >= 0) {
				slideTo(queue, queueDirection);
				queue = -1;
			} else {
				setTimer();
			}
		});
	}
		
	function slideTo(num,direction) {
		stopTimer();
		if(!transition) {
			transition = true;
			if(direction == "left") {
				$slides.eq(current).animate({left: -958}, TransitionSpeed, 'easeInOutSine');
				$slides.eq(num).css({left: 958}).animate({left: 0}, TransitionSpeed, 'easeInOutSine', function() {
					finishAnimation(num);
				});
			}
			else {
				$slides.eq(current).animate({left: 958}, TransitionSpeed, 'easeInOutSine');
				$slides.eq(num).css({left: -958}).animate({left: 0}, TransitionSpeed, 'easeInOutSine', function() {
					finishAnimation(num);
				});
			}
		} else {
			queue = num;
			queueDirection = direction;
		}
	}
		
	function nextSlide() {
		currentNum = currentNum + 1;
		if(currentNum >= $slides.length) {
			currentNum = 0;
		}
		slideTo(currentNum,"left");
	}
	
	$homeslide.find('span.leftArrow').click(function () {
		currentNum = currentNum - 1;
		if(currentNum < 0) {
			currentNum = $slides.length - 1;
		}
		slideTo(currentNum,"right");
	});
	$homeslide.find('span.rightArrow').click(function () {
		nextSlide();
	});
		
	setTimer();
	
	if(jQuery.browser.msie && jQuery.browser.version < 9) {
		$content.css({background: "none"});
	}
}

jQuery.extend( jQuery.easing,
{
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	}
});
