$(document).ready(function() {
	
	// Track clicks on external links using the Google Analytics async code
	$("a[href^='http://']").click(function(){
		var href = $(this).attr('href');

		if ( (href.match(/^http/)) && (! href.match(document.domain)) ) {
			_gaq.push(['_trackEvent', 'links', 'external', href, 0])
		}
	});

	// Darken the search box text when clicked
	$(".search .input-text").focus(function(){
		$('.search .input-text').addClass('input-text-active');
	});
	
	// Refresh the price on pageload and after
	// relevant form elements are clicked.
	$(".shopform input:checkbox").click(calculateTotal);
	$(".shopform #numLocations").change(calculateTotal);
	
	// Lead form
	$('.leadcheckout.passform').css("display","none"); 
	$('.leadcheckout #saveCard').click(function() { 
		$('.leadcheckout .passform').slideToggle('slow');
	});
	
	// Home page "Show More Cities" link
	$('.cities_toggle').click(function() {
		$('#fewcities').slideToggle('normal');
		$('#morecities').slideToggle('normal');
		$('#show_more_cities').toggle();
		$('#hide_more_cities').toggle();

		return false;
	});

	// Add Shop Country Links
	$('#showadd_usa').click(function() {
		$('#addform_canada').slideUp('normal');
		$('#addform_usa').slideDown('normal');
		return false;
	});

	$('#showadd_canada').click(function() {
		$('#addform_usa').slideUp('normal');
		$('#addform_canada').slideDown('normal');
		return false;
	});
    	
	// Modal window pop-up for SMS function		
    $("a#sms_modal").click(function(e){
        e.preventDefault();
        var id = $("#dialog");
        
        dialog_html = $(id).html();
        
        //Get the screen height and width  
        var maskHeight = $(document).height();  
        var maskWidth = $(window).width();  
      
        //Set height and width to mask to fill up the whole screen  
        $('#mask').css({'width':maskWidth,'height':maskHeight,'top':0, 'left': 0, 'opacity': 0.8});     
      
        //Get the window height and width  
        var winH = $(window).height();  
        var winW = $(window).width();  
                
        //Set the popup window to center  
        $(id).css('top',  winH/2-$(id).height()/2);  
        $(id).css('left', winW/2-$(id).width()/2);  
      
        //transition effect  
        $(id).fadeIn();   
        $('#mask').fadeIn();      
    });
    
    //if close button is clicked  
    $('.window a.close').click(function (e) {  
        //Cancel the link behavior  
        e.preventDefault();  
        reset_window();
    });       
      
    //if mask is clicked  
    $('#mask').click(function () {  
        reset_window();
    });
    
    // Ajax method for SMS form submit    
    $("form#sms_form").submit(function(e){
        // arrest default behavior of button
        e.preventDefault();
        // remove error/success classes from response div
        $("div.window #response").removeClass('success error');
        // send the request and process result
        $.post(
            '/sms/send',
            {sms_number: $("form#sms_form input[name='sms_number']").val(),
            shop_id: $("form#sms_form input[name='shop_id']").val(),
            sms_text: $("form#sms_form textarea[name='sms_text']").val()},
            function (data) {
                var out = data.split(' | ');
                // return results to .window
                if(out[0] == 'SUCCESS') {
                    $("div.window #response").addClass('success');
                } else {
                    $("div.window #response").addClass('error');
                }
                $("div#dialog").css('height', '375px');
                $("div.window #response").html(out[1]);
            }
        );
    }); 
});


// Calculate price for display
// This logic mirrors the actual price
// calculation function
function calculateTotal() {
	var numLocations = parseInt($("#numLocations").val());
	var numBrands1 = parseInt($("#makes1 input:checked").length);
	var numBrands2 = parseInt($("#makes2 input:checked").length);
	var numBrands3 = parseInt($("#makes3 input:checked").length);
	
	var amount1 = numLocations * numBrands1 * 35;
	if (amount1 > 0) {
		amount1 = amount1 + 22;
	}

	var amount2 = numLocations * numBrands2 * 35;
	if (amount2 > 0) {
		amount2 = amount2 + 0;
	}

	var amount3 = numLocations * numBrands3 * 20;
	if (amount3 > 0 && amount1 == 0 && amount2 == 0) {
		amount3 = amount3 + 10;
	}

	var total = amount1 + amount2 + amount3;

	$('#dispAmount').text(total);
	$('#dispAmount').val(total);
};

function reset_window() {
    $('#mask, .window').hide();
    $('.window #response').html('');
    $('.window #response').removeClass('error success');
}

function populateAddress(shopid) {
	$.getJSON('/admin/jsonshopaddr/' + shopid, function(json) {
		// alert("JSON Data: " + json[0].name);
		$("#shopid").val(json[0].shopid);		
		$("#shopName").val(json[0].name);
		$("#address").val(json[0].address);
		$("#city").val(json[0].city);
		$("#state").val(json[0].state);
		$("#zip").val(json[0].zip);
	});
}
