// JavaScript Document

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
	
function startTimer() {
	rfc = setTimeout("ajaxFunction('refresh_cart_count')",1000);
	//getInboxTime = setTimeout("ajaxFunction('update_inbox')",10000);
	}

function ajaxFunction(ty,idx,idx2) {
	var ajaxRequest;	
	try { ajaxRequest = new XMLHttpRequest(); } 
	catch (e){
		try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e) {
			try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } 
			catch (e){
				alert("Your browser broke!");
				return false;
				}
			}
		}	
	if (ty == "fb_login") {
		var url = "scripts.php?action=fb_login";
	}
	else if (ty == "refresh_cart_count") {
		var url = "scripts.php?action=refresh_cart_count";
	}
	else if (ty == "resetter") {
		var url = "scripts.php?action=login_form_resetter";
	}
	else if (ty == "check_user_name") {
		var url = "scripts.php?action=check_user_name&user_name=" + escape(document.getElementById('reg_user_name').value);
	}
	else if (ty == "check_user_password") {
		var url = "scripts.php?action=check_user_password&user_password=" + escape(document.getElementById('user_password').value);
	}
	else if (ty == "check_user_verifypass") {
		var user_password = escape(document.getElementById('user_password').value);
		var verify_password = escape(document.getElementById('verify_password').value);
		var url = "scripts.php?action=check_user_verifypass&user_password=" + user_password + "&verify_password=" + verify_password;
	}
	else if (ty == "check_user_email") {
		var url = "scripts.php?action=check_user_email&user_email=" + escape(document.getElementById('user_email').value);
	}
	else if (ty == "add_to_cart") {
		var i = idx;	
		//var qty = escape(document.getElementById('item_quantity' + idx2).value);		
		//document.getElementById('item' + idx2).style.background = "#6699CC";
		var url = "scripts.php?action=add_to_cart&item=" + i;
	}
	else if (ty == "add_to_cart_IS") {
		var i = idx;	
		var qty = escape(document.getElementById('item_quantity' + idx2).value);	
		var avail = document.getElementById('available' + idx2).innerHTML;
		if (parseInt(qty) > parseInt(avail)) {
			alert("Sorry, we have no more :(");
			return false;
		}
		else {
			var url = "scripts.php?action=add_to_cart&item=" + i + "&qty=" + qty;
		}
	}
	else if (ty == "add_pull") {
		var i = escape(idx);	
		var url = "scripts.php?action=add_pull&item=" + i + "&qty=1";
	}
	else if (ty == "dq_pull") {
		var i = idx;	
		var url = "scripts.php?action=dq_pull&item=" + i;
		var new_qty = parseInt(document.getElementById('pull' + idx2).innerHTML) - 1;
			if (new_qty <= 0) { 
				document.getElementById('pull' + idx2).innerHTML = 0; 
			}
			else { 
				document.getElementById('pull' + idx2).innerHTML = new_qty; 
			}
	}
	else if (ty == "del_pull") {
		var i = idx;	
		var url = "scripts.php?action=del_pull&item=" + i;
	}
	else if (ty == "alter_cart") {
		var url = "scripts.php?action=" + ty + "&item=" + idx + "&x=" + idx2;
	}
	else if (ty == "pre_payment") {
		var ord = escape(document.getElementById('order_number').value);
		var rt = escape(document.getElementById('running_total').value);
		var mt = escape(document.getElementById('merch_total').value);
		var sh = escape(document.getElementById('shipping').value);
		var tx = escape(document.getElementById('tax').value);
		var gr = escape(document.getElementById('grouping').value);
		var url = "scripts.php?action=pre_payment&rt=" + rt + "&mt=" + mt + "&sh=" + sh + "&tx=" + tx + "&ord=" + ord + "&gr=" + gr;
	}
	
		
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null);

	ajaxRequest.onreadystatechange = function() {
		if (ajaxRequest.readyState == 4){
			if (ty == "fb_login") {
				location.href = ajaxRequest.responseText.trim();
			}
			else if (ty == "add_to_cart_IS") {
				document.getElementById('item' + idx2).style.background = "#6699CC";
				document.getElementById('available' + idx2).innerHTML = document.getElementById('available' + idx2).innerHTML - qty;
			}			
			else if (ty == "alter_cart") {
				location.href = ajaxRequest.responseText;
			}
			else if (ty == "logout") {
				document.getElementById('login').innerHTML = ajaxRequest.responseText;
			}
			else if (ty == "resetter") {
				document.getElementById('login').innerHTML = ajaxRequest.responseText;
			}
			else if (ty == "check_user_name") {
				document.getElementById('user_name_check_response').innerHTML = ajaxRequest.responseText;
			}
			else if (ty == "check_user_password") {
				document.getElementById('user_password_check_response').innerHTML = ajaxRequest.responseText;
			}
			else if (ty == "check_user_verifypass") {
				document.getElementById('user_verifypass_check_response').innerHTML = ajaxRequest.responseText;
			}
			else if (ty == "check_user_email") {
				document.getElementById('user_email_check_response').innerHTML = ajaxRequest.responseText;
			}
			else if (ty == "refresh_cart_count") {
				if (document.getElementById('itmcnt')) {
					document.getElementById('itmcnt').innerHTML = ajaxRequest.responseText.trim();
				}
				startTimer();
				}
			else if (ty == "add_pull") {
				document.getElementById('pull' + idx2).innerHTML = ajaxRequest.responseText.trim();
			}
			else if (ty == "add_to_cart") {
				document.getElementById('cart_item' + idx2).innerHTML = ajaxRequest.responseText.trim();
			}
			else if (ty == "del_pull") {
				location.href = ajaxRequest.responseText.trim();
			}
			else if (ty == "pre_payment") {
				if (ajaxRequest.responseText.trim() == "") {
					//alert("hello");
					document.getElementById('payment').submit();
				}
				else {
					alert(ajaxRequest.responseText.trim());
					location.href = "error.php?m=" + ajaxRequest.responseText.trim();
				}
			}
		}	
	}
}
	
function show(me) {
	var thing = document.getElementById(me);
	if (thing.style.display == "block") {
		thing.style.display = "none";
		}
	else {
		thing.style.display = "block"; 
		}
	}
	
function show_order(me) {
	var thing = document.getElementById(me);
	if (thing.style.display == "block") {
		thing.style.display = "none";
		document.getElementById('slappy').src = "images/icons/balloonica/24x24/folder_open.png";
		}
	else {
		thing.style.display = "block";
		document.getElementById('slappy').src = "images/icons/balloonica/24x24/folder.png";
		}
	}
	
function show_fp() {
	document.getElementById('login').innerHTML = 
			"<div id=\"fetch_password\" style=\"float: left; margin: 10px; text-align: center;\">"
			+ "<label for=\"check_email\" style=\"margin-right: 10px;\">Enter your email:</label>"
			+ "<input class=\"formtreb\" type=\"text\" value=\"\" id=\"check_email\" name=\"check_email\" style=\"margin-right: 10px;\" />"
			+ "<input class=\"formtreb\" type=\"button\" value=\"submit\" onClick=\"ajaxFunction('check_email'); show('fetch_password'); show('check_email_result');\" />"
			+ "<p><a href=\"index.php\" class=\"treb\">cancel</a></p>"
			+ "</div>";
}

function doSearch(){
	//var search_genre = document.getElementById('search_genre').value;
	//var search_brand = document.getElementById('search_brand').value;
	var search_category = document.getElementById('search_category').value;
	var search_page = document.getElementById('search_page').value;
	if (document.getElementById('search_keywords').value == "keyword search") {
		var search_keywords = "";
	}
	else {
		var search_keywords = escape(document.getElementById('search_keywords').value);
	}
	//location.href = "show_items.php?genre=" + search_genre + "&brand=" + search_brand + "&category=" + search_category + "&page=" + search_page + "&kw=" + search_keywords;
	location.href = "show_items.php?category=" + search_category + "&page=" + search_page + "&kw=" + search_keywords;
	}
			
$.fn.infiniteCarousel = function () {

	function repeat(str, num) {
		return new Array( num + 1 ).join( str );
	}

	return this.each(function () {
		var $wrapper = $('> div', this).css('overflow', 'hidden'),
		$slider = $wrapper.find('> ul'),
		$items = $slider.find('> li'),
		$single = $items.filter(':first'),

		singleWidth = $single.outerWidth(), 
		visible = Math.ceil($wrapper.innerWidth() / singleWidth), 
		// note: doesn't include padding or border
		currentPage = 1,
		pages = Math.ceil($items.length / visible);            


		// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
		if (($items.length % visible) != 0) {
			$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
			$items = $slider.find('> li');
		}

		// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
		$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
		$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
		$items = $slider.find('> li'); // reselect

		// 3. Set the left position to the first 'real' item
		$wrapper.scrollLeft(singleWidth * visible);

		// 4. paging function
		function gotoPage(page) {
			var dir = page < currentPage ? -1 : 1,
			n = Math.abs(currentPage - page),
			left = singleWidth * dir * visible * n;

			$wrapper.filter(':not(:animated)').animate({
			scrollLeft : '+=' + left
			}, 500, function () {
			if (page == 0) {
				$wrapper.scrollLeft(singleWidth * visible * pages);
				page = pages;
			} else if (page > pages) {
				$wrapper.scrollLeft(singleWidth * visible);
				// reset back to start position
				page = 1;
			} 

			currentPage = page;
		});                

		return false;
	}

	$wrapper.after('<a class="arrow back"></a><a class="arrow forward"></a>');

	// 5. Bind to the forward and back buttons
	$('a.back', this).click(function () {
		return gotoPage(currentPage - 1);                
	});

	$('a.forward', this).click(function () {
		return gotoPage(currentPage + 1);
	});

	// create a public interface to move to a specific page
	$(this).bind('goto', function (event, page) {
		gotoPage(page);
	});
	});  
};

$(document).ready(function () {
	$('.infiniteCarousel').infiniteCarousel();
});			