$(document).ready(function() {
   $('#column_nav li').hover(function(){
		var idx = $('#column_nav li').index(this);
	       $('#column_nav li a').eq(idx).css({ color : "#777" });
	 }, function(){
		var idx = $('#column_nav li').index(this);
	       $('#column_nav li a').eq(idx).css({ color : "#000" });
	 });

	 $('#column_nav li').click(function(){
		var idx = $('#column_nav li').index(this);
	       window.location.replace($('#column_nav li a').eq(idx).attr('href'));
	 });

	 $('#get_buyer').click(function(){
		get_user_orders();
	 });

	 $('#email_mailing_list').click(function(){
		email_mailing_list();
	 });

	 $('#mailing_list').click(function(){
		get_mailing_list();
	 });


	 $('#login').click(function(){
		validate_login();
	 });

	 $('#fulfill_subscriptions').click(function(){
		fulfill_subscriptions();
	 });

	 $('#email_paper').click(function(){
		email_paper();
	 });

	 $('#receipts').click(function(){
		receipts();
	 });

	 $('#upgrade_order').click(function(){
		upgrade_order();
	 });

	 $('#make_thumbs').click(function(){
		make_thumbs();
	 });

	 $('#show_thumbs').click(function(){
		show_thumbs();
	 });

	 $('#event_reports').click(function(){
		event_reports();
	 });

	 $('#subscription_reports').click(function(){
		subscription_reports();
	 });

	$('#column_nav li').mouseover(function(){
		$('#li').css({textColor : "#ffffff" });
	});

	$('#thumb_bvi img').mouseover(function(){
		$('#thumb_bvi').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg_hv.jpg)" });
	 });
	 $('#thumb_bvi img').mouseout(function(){
		$('#thumb_bvi').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg.jpg)" });
	 });
	 $('#thumb_bto img').mouseover(function(){
		$('#thumb_bto').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg_hv.jpg)" });
	 });
	 $('#thumb_bto img').mouseout(function(){
		$('#thumb_bto').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg.jpg)" });
	 });
	 $('#thumb_bfv img').mouseover(function(){
		$('#thumb_bfv').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg_hv.jpg)" });
	 });
	 $('#thumb_bfv img').mouseout(function(){
		$('#thumb_bfv').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg.jpg)" });
	 });
	 $('#thumb_bpc img').mouseover(function(){
		$('#thumb_bpc').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg_hv.jpg)" });
	 });
	 $('#thumb_bpc img').mouseout(function(){
		$('#thumb_bpc').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg.jpg)" });
	 });

	 $('#thumb_be img').mouseover(function(){
		$('#thumb_be').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg_alt_hv.jpg)" });
	 });
	 $('#thumb_be img').mouseout(function(){
		$('#thumb_be').css({ backgroundImage : "url(<?php echo SERVER_ROOT;?>/img/thumb_bg_alt.jpg)" });
	 });
 });

function email_mailing_list(){
	$.post(serverRoot + "inc/admin_functions.php", {type: "email_mailing_list", a: "reports"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function get_mailing_list(){
	$.post(serverRoot + "inc/admin_functions.php", {type: "mailing_list", a: "reports"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function event_reports(){
	$.post(serverRoot + "inc/admin_functions.php", {type: "events", a: "reports"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function fulfill_subscriptions(){
	$.post(serverRoot + "inc/admin_functions.php", {type: "", a: "fulfill_subscriptions"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function subscription_reports(){
	$.post(serverRoot + "inc/admin_functions.php", {type: "subscriptions", a: "reports"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function email_paper(){
	$.post(serverRoot + "inc/admin_functions.php", {a: "email_paper"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function receipts(){
	$.post(serverRoot + "inc/admin_functions.php", {a: "receipts"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function upgrade_order(){
	$.post(serverRoot + "inc/admin_functions.php", {a: "upgrade_order"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function make_thumbs(){
	alert('here');
	$.post(serverRoot + "inc/admin_functions.php", {a: "make_thumbs"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}

function show_thumbs(){
	$.post(serverRoot + "inc/admin_functions.php", {a: "show_thumbs"}, function(data){
		document.getElementById("tool_div").innerHTML = data;
	});
}


function validate_login(){

	u_name = document.getElementById('username').value;
	p_word = document.getElementById('password').value;

	$.post(serverRoot + "inc/admin_functions.php", {username: ""+u_name+"", password: ""+p_word+"", a: "validate_login"}, function(data){
		if(data == 'SUCCESS'){
			location.reload(true);
		} else {
			document.getElementById("login_error").innerHTML = data;
		}
	});
}

function email_receipt(key){
	email = document.getElementById('user_email').value;

	$.post(serverRoot + "inc/admin_functions.php", {email: ""+email+"", key: ""+key+"", a: "email_receipt"}, function(data){
		document.getElementById("email_confirmation").innerHTML = data;
	});
}

function add_months(product, year, path){
	$.post(path + "script/month_update.php", {product: ""+product+"", year: ""+year+""}, function(data){
	document.getElementById("month_select").innerHTML = data;
	});
}

function get_user_orders(){

	email = document.getElementById('user_email').value;
	$.post(serverRoot + "inc/admin_functions.php", {email: ""+email+"", a: "get_user_order"}, function(data){
	document.getElementById("customer_data").innerHTML = data;
	});
}

function change_province(country, option){

	$.post("../script/province.php", {country: ""+country+"", option: ""+option+""}, function(data){
		document.getElementById(option + "province_select").innerHTML = data;
	});

	if(country == 'US'){
		document.getElementById(option + "postal_name").innerHTML = 'Zip Code';
	} else {
		document.getElementById(option + "postal_name").innerHTML = 'Postal Code';
	}

}

function toggle_swap(state, toggle){
	$.post("script/toggle.php", {toggle: ""+toggle+"", state: ""+state+""}, function(data){

		document.getElementById('shipping').innerHTML = data;
	});
}


//If the browser is W3 DOM compliant, execute setImageSwaps function
if (document.getElementsByTagName && document.getElementById) {
if (window.addEventListener) window.addEventListener('load', setImageSwaps, false);
else if (window.attachEvent) window.attachEvent('onload', setImageSwaps);
}

//When document loads, apply the prepareImageSwap function to various images with our desired settings
function setImageSwaps() {
//Mousedown, restore - for images in container with ID=example2
prepareImageSwap('example2',true,true,true,true);
//Hover, mousedown, no restore - for images in container with ID=example3
prepareImageSwap('example3',true,false,true,false);
//Hover with restore, most basic usage - for any image in document.body that are not yet processed (function accepts elements,too)
prepareImageSwap(document.body);
//Note that once an image is processed, it won't be processed again, so you should set more specific images first, e.g. document.body, as it is the grand
//container, has to be processed last.
}


//The following is the function that do the actual job
prepareImageSwap(document.body);

function prepareImageSwap(elem,mouseOver,mouseOutRestore,mouseDown,mouseUpRestore,mouseOut,mouseUp) {
//Do not delete these comments.
//Non-Obtrusive Image Swap Script by Hesido.com
//V1.1
//Attribution required on all accounts
	if (typeof(elem) == 'string') elem = document.getElementById(elem);
	if (elem == null) return;
	var regg = /(.*)(_nm\.)([^\.]{3,4})$/
	var prel = new Array(), img, imgList, imgsrc, mtchd;
	imgList = elem.getElementsByTagName('img');

	for (var i=0; img = imgList[i]; i++) {
		if (!img.rolloverSet && img.src.match(regg)) {
			mtchd = img.src.match(regg);
			img.hoverSRC = mtchd[1]+'_hv.'+ mtchd[3];
			img.outSRC = img.src;
			if (typeof(mouseOver) != 'undefined') {
				img.hoverSRC = (mouseOver) ? mtchd[1]+'_hv.'+ mtchd[3] : false;
				img.outSRC = (mouseOut) ? mtchd[1]+'_ou.'+ mtchd[3] : (mouseOver && mouseOutRestore) ? img.src : false;
				img.mdownSRC = (mouseDown) ? mtchd[1]+'_md.' + mtchd[3] : false;
				img.mupSRC = (mouseUp) ? mtchd[1]+'_mu.' + mtchd[3] : (mouseOver && mouseDown && mouseUpRestore) ? img.hoverSRC : (mouseDown && mouseUpRestore) ? img.src : false;
				}
			if (img.hoverSRC) {preLoadImg(img.hoverSRC); img.onmouseover = imgHoverSwap;}
			if (img.outSRC) {preLoadImg(img.outSRC); img.onmouseout = imgOutSwap;}
			if (img.mdownSRC) {preLoadImg(img.mdownSRC); img.onmousedown = imgMouseDownSwap;}
			if (img.mupSRC) {preLoadImg(img.mupSRC); img.onmouseup = imgMouseUpSwap;}
			img.rolloverSet = true;
		}
	}

	function preLoadImg(imgSrc) {
		prel[prel.length] = new Image(); prel[prel.length-1].src = imgSrc;
	}

}

function imgHoverSwap() {this.src = this.hoverSRC;}
function imgOutSwap() {this.src = this.outSRC;}
function imgMouseDownSwap() {this.src = this.mdownSRC;}
function imgMouseUpSwap() {this.src = this.mupSRC;}
