//Doc Ready functions go here
$(function() {
	$(".nv-mn img").hover(
		function () {
			var img_orig = $(this).attr("src");
			var img_replace = img_orig.replace("_off.png", "_on.png");
			$(this).attr( "src", img_replace )
		},
		function () {
			if($(this).parent().hasClass("nv-mn")){
				var img_orig = $(this).attr("src");
				var img_replace = img_orig.replace("_on.png", "_off.png");
				$(this).attr( "src", img_replace )
			}
		}
	);
	
	updateStoreTop();
});

function updateStoreTop(){	
	AjaxCall('/_ajax/store/?action=cartCount', "quick_nav_cart_count",
			function(){
				if($("#quick_nav_cart_count").html() > 0){
					$("#quick_nav_cart").show()
				}else{
					$("#quick_nav_cart").hide()
				}	
			} )	
}

function callEventsBox(id){
	jQuery.facebox({ ajax: '/_ajax/rsvp/?id=' + id});
}

function callSubscribeBox(){
	jQuery.facebox({ ajax: '/_ajax/subscribe/'});
}

function submitSubscribeBox(){
	url = '/_ajax/subscribe/?action=submit';
	
	$.post(url, 
		{ 
			firstname: $("#firstname").val(),
			email: $("#email").val()
		},
	  	function(data){
	  		$("#fbContent").html(data);
		}
	);
}

function submitEventsBox(id){
	url = '/_ajax/rsvp/?id=' + id + "&action=submit";
	
	$.post(url, 
		{ 
			firstname: $("#firstname").val(),
			lastname: $("#lastname").val(),
			email: $("#email").val(),
			contact_number: $("#contact_number").val(),
			guests: $("#guests").val()
		},
	  	function(data){
	  		$("#fbContent").html(data);
		}
	);
}

function mainNavSet(obj){
	if(obj!=""){
		$(function() {
			var img_orig = $(obj + " img").attr("src");
			var img_replace = img_orig.replace("_off.png", "_on.png");
			$(obj + " img").attr( "src", img_replace );
			$(obj).removeClass("nv-mn");
		});
	}
	
}

function sideNavSet(obj){
	$(function() {
		$(obj).addClass("onside");
	});
}


function initBGImages(option, imgJS){
	$(function () {
	   $('#fadingImages').imagefader({
            fadeSpeed: 3000,
			displayTime: 5000,
			imgArr: imgJS,
	        callBackFade: function (pageID, blockOpacity){
	        	if(option == "home"){
	        		if(pageID == 1){
	        			$('#content').fadeIn(3000);
	        		}
	        	}else{
	        		if(blockOpacity){
	        			$(".over_block").fadeTo(1000, blockOpacity/100);
	        		}
	        	}
	        }
        });
        
    });
}

function ____initBGImages(option, play){
	$(function () {
		if(play == true){
		   $('.anythingSlider').anythingSlider({
	            easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
	            autoPlay: play,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
	            delay: 8000,                    // How long between slide transitions in AutoPlay mode
	            startStopped: false,            // If autoPlay is on, this can force it to start stopped
	            animationTime: 1300,            // How long the slide transition takes
	            hashTags: false,                // Should links change the hashtag in the URL?
	            buildNavigation: false,         // If true, builds and list of anchor links to link to each slide
	    		pauseOnHover: false,            // If true, and autoPlay is enabled, the show will pause on hover
	    		startText: "Go",             	// Start text
		        stopText: "Stop",               // Stop text
		        navigationFormatter: null,       // Details at the top of the file on this use (advanced use)
		        onSlideComplete: function (pageID, blockOpacity){
		        	if(option == "home"){
		        		if(pageID == 2){
		        			$('#content').fadeIn('slow');
		        		}
		        	}else{
		        		if(blockOpacity){
		        			$(".over_block").fadeTo(1000, blockOpacity/100);
		        		}
		        	}
		        }
	        });
	
	    	$(document).keydown(function(e) {
				if (e.keyCode == '32') {
					e.preventDefault();
					$('.anythingSlider').anythingSlider();
				}
			});	
        }
        
    });
}

function AjaxCall(url, target, callback ){
	if(url != "" && target != ""){
		$.get(url, { ajax: "true"},
		  function(data){
		  	$("#"+target).html(data);
		  	
		  	if (typeof(callback) == 'function') {
				callback();
			}
		});
	}
}

function submitForm(theform, theaction){
	theform.action = theaction;
	theform.submit();
}

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

function goTopOfPage(){
	$('html, body').animate({scrollTop:0}, 'slow');
}


function toggleTable(el,st) {
	if(st == 1){
		$("#"+el).show();
	}else{
		$("#"+el).hide();
	}	
}

function formatCurrency(num) {
	num = num.toString().replace(/\R|\,/g,'');
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		
	return (((sign)?'':'-') + 'R' + num + '.' + cents);
}


//function that can be used to determine if an element exits
//usage: 
jQuery.fn.exists = function(){
	return jQuery(this).length>0;
}


/*Handlers for on off state images*/
//Replaces an off image with an on image e.g. button_off.gif -> button_on.gif
$.fn.onImage = function ()
{
    $(this).each(function ()
    {
        $(this).attr('src', $(this).attr('src').replace('_off.', '_on.'));
        return $(this);
    });
}

//Replaces an on image with an off image e.g. button_on.gif -> button_off.gif
$.fn.offImage = function ()
{
    $(this).each(function ()
    {
        $(this).attr('src', $(this).attr('src').replace('_on.', '_off.'));
        return $(this);
    });
    
}

function sleep(numberMillis)
{
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true)
	{
		now = new Date();
		if (now.getTime() > exitTime)
		return;
	}
}
