$(document).ready(function() {
	mouseover_categories();
	mouseover_top_navigation();
	//preload images
	//$.preloadCssImages();
	$('body').live('click', log_click);
}); 

var orig = '';
var clear_keywords = true;
var orig_top = '';

function mouseover_categories() {
	var orig_top = $('#main_frame_top').css('background-image');

	$('ul.categories li').mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); }).click(function() { $(this).addClass('clicked'); });

	$('ul.navigation li').mouseover(function() { 
		$(this).addClass('hover'); 
		var link = $(this).children().attr('href');
		link = link.substr(link.length-11, 11);

		if(link=='contact.php') {
			//get current top frame
			var main_frame = $('#main_frame_top');
			var frame_image = main_frame.css('background-image');
			frame_image = frame_image.replace('-Contact-Us', '');
			// see if we need to deal with quotes or not
			if(frame_image.substr(0, 5) == 'url("') {
				frame_image = frame_image.substr(0, frame_image.length - 6);
				frame_image = frame_image + '-Contact-Us.png")';

			} else {

				frame_image = frame_image.substr(0, frame_image.length - 5);
				frame_image = frame_image + '-Contact-Us.png)';
			}
			main_frame.css('background-image', frame_image);
		}

	}).mouseout(function() { 
		$(this).removeClass('hover'); 
		$('#main_frame_top').css('background-image', orig_top);

	}).click(function() { $(this).addClass('clicked'); });

	$('ul.checkout_button li').mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); }).click(function() { $(this).addClass('clicked'); });
	
    $('input.search_submit').mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); }).click(function() { $(this).addClass('clicked'); });
    
    $('#current_shopping_cart_info a, #shopping_cart_icon_link').mouseover(function() { 
    /* replace img with black cart image */ 
    $('#current_shopping_cart_info').addClass('hover');
    orig = $('#shopping_cart_icon_link img').attr('src');
    var new_img = orig.replace('White','Black');
    $('#shopping_cart_icon_link img').attr('src', new_img);
    }).mouseout(function() { $(this).removeClass('hover');
    $('#shopping_cart_icon_link img').attr('src', orig);
    $('#current_shopping_cart_info').removeClass('hover');
    }).click(function() { /* restore to previous image */ (this).addClass('clicked'); });
    
    //when search box is initially click, clear the contents
    $('#keyword_search').focus(clear_search_keywords).click(clear_search_keywords);
		$('.brown').live('mouseover',(function() {
			$(this).addClass('hover');
		})).live('mouseout', (function() {
			$(this).removeClass('hover');
		}));
}
function clear_search_keywords() {

    if(clear_keywords) {
        $('#keyword_search').attr('value', '');
        clear_keywords = false;
    }
}
function mouseover_top_navigation() {
	$('ul.navigation li').mouseover(function() { $(this).addClass('navigation_hover'); }).mouseout(function() { $(this).removeClass('navigation_hover'); });

}

function get_cookie(key) {

	var nameEQ = key + "=";
	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 set_cookie(key, value) {
		var days = 1;
		var date = new Date();
		var domain = document.domain;
		
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();

		var str = key + "=" + value + "; expires=" + date.toGMTString() + "; path=/;domain=" + domain + ";";
		document.cookie = str;

}

var version;
var browser;
	
function log_click(e) {

	//get the page offset and compensate for it.

	
	if(!document.getElementById('log_click')) {
		$('body').append('<div id="log_click">&nbsp;</div>');
	}	

	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="version") {
			version = val;
		} else if(val){
			browser = i;
		}
	});
	var offset = document.getElementById('page').offsetLeft;
	var host = (("https:" == document.location.protocol) ? "https://www." : "http://www.");

	$('#log_click').load(host + 'completepartysupplies.com/click.php?x='+(e.pageX-offset)+'&y='+e.pageY+'&b='+browser+'&v='+version);
	
}
