$(document).ready(function () {
	
	if($('#navigation') !== undefined){
	
		// Load navigation
		$('#navigation').load(path + 'includes/navigation.php', 'path=' + path, function (){
			
			//alert($('#navigation').text());
			
			init_navigation();
			
			tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
			
			// Popular add ons widget
			// target, number_of_pages, page_size, buffer, listing_size, 
			init_add_on('popular', 3, 3);
			
			// Cart add ons widget
			// target, number_of_pages, page_size, buffer, listing_size, 
			init_add_on('cart', 4, 2);
			
			init_quick_add();
			
			customSearchControl_support.draw('cse_support');
			customSearchControl_contact.draw('cse_contact');
			
			
		});
		
	}
	
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
	// Social
	twttr.anywhere(function(T){
		T('.twitter').followButton("produnkhoops");
	});
	
	/*
	$('#social_open a').click(function(){
		$('#social_closed').toggle();
		$('#social_open').toggle();
	});
	
	$('#social_closed').click(function(){
		$('#social_closed').toggle();
		$('#social_open').css('display', 'inline-block');
		$('#twitter iframe').css({width:236,height:24});
	});
	*/
		
});

function debug(msg){
	$('#debug').html($('#debug').html() + "<br/>" + msg);
}

jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function init_quick_add(target){
	
	if(target) target += ' ';
	
	$(target + '.quick_add').hover(
		function(){
			$(this).css('backgroundImage', 'url(' + path + 'images/design/cart/add_to_cart_active.gif)');
		},
		function(){
			$(this).css('backgroundImage', 'url(' + path + 'images/design/cart/add_to_cart_inactive.gif)');
		}).click(function(){
			
			//alert($(this).attr('page_id'));
			me = $(this);
			
			// Check to see if this product needs configuration
			
			$.ajax({
				type: "GET",
			   	url: path + "includes/quick_configuration.php",
			   	data: "check=1&page_id=" + me.attr('page_id'),
			   	success: function(msg){
					
					// Pull up configuration
					if(msg == '1'){
						tb_show("Product Configuration",path + "includes/quick_configuration.php?page_id=" + me.attr('page_id') + "&height=390&width=550",true);
					
					// Let user know it was added
					}else{
						me.css('backgroundImage', 'url(' + path + 'images/design/cart/add_to_cart_success.gif)');
						setTimeout(function(){
							me.css('backgroundImage', 'url(' + path + 'images/design/cart/add_to_cart_inactive.gif)');
						},5000);
						init_cart_summary();
					}

			   	}
			 });
			
		});
}

function add_to_cart(page_id){
	
	str_post = '';
	$('#price_form input, #price_form select').each(function(i){
		str_post += "&" + $(this).attr('name') + "=" + $(this).val();
	});
	
	me = $("div[page_id='" + page_id + "']");
	
	$.ajax({
		type: "POST",
		url: path + "cart/view.php",
		data: str_post,
		success: function(msg){
			me.css('backgroundImage', 'url(' + path + 'images/design/cart/add_to_cart_success.gif)');
			setTimeout(function(){
				me.css('backgroundImage', 'url(' + path + 'images/design/cart/add_to_cart_inactive.gif)');
			},5000);
			init_cart_summary();
			tb_remove();
		}
	});
}

function isInteger(s){
	var i;
	if (isEmpty(s))
	if (isInteger.arguments.length == 1) return 0;
	else return (isInteger.arguments[1] == true);
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

function isEmpty(s){
	return ((s == null) || (s.length == 0))
}
function isDigit (c){
	return ((c >= "0") && (c <= "9"))
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/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)?'':'-') + '$' + num + '.' + cents);
}

function start_Chat(){
	window.open('http://www.websitealive5.com/642/rRouter.asp?groupid=642&departmentid=','guest','width=450,height=400');
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name)
{
	
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}