﻿// JavaScript Document

$.urlParam = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

// Cached category names
var categoryName = new Array();
//var originalUrl = 'http://202.142.221.142/~handtoy/data/;
var originalUrl = 'http://www.handtoy.com/data/';

function retrieveMenu() {
	$('div#body_menu').hide();
	$.getJSON(originalUrl + 'getmenu.php', function(data) {
		if (data) {
			var menu = '';
			for(var i=0; i<data.length; i++) {
				menu += '<div id=\'' + data[i].type + data[i].cid + '\' class=\'menu\'>';
				menu += '<img src=\'' + data[i].imguri + '\' /> ';
				menu += data[i].name;
				menu += '<br />';
				menu += '</div>';
			}
			$('div#body_menu_content').html(menu).show();
			$('div#body_menu').slideDown();
			for(var i=0; i<data.length; i++) {
				var ele = $('div#body_menu_content div#' + data[i].type + data[i].cid);
				ele.hover(
				  function () {
					$(this).fadeOut(50);
					$(this).fadeIn(50);
					$(this).addClass("menuhover");
				  },
				  function () {
					$(this).removeClass("menuhover");
				  }
				);
				
				if (data[i].type == 'c') {
					ele.click(function() { retrieveContent(this.id); });
				} else {
					// Cache category names for later use
					categoryName[data[i].cid] = data[i].name;
					
					ele.click(function() { retrieveProductsInCategory(this.id); });
				}
			}
		}
	});
}

function togglePromotionContent(id) {
	if (id == '63') {
		$('div#body_content_content_promote').show();
	} else {
		$('div#body_content_content_promote').slideUp();
	}
}

function getMemberPrice(p) {
    return (p * 0.85).toFixed(2);
}

function retrieveContent(id) {
	id = id.replace('c', '');
	$('div#body_content_content').slideUp();
	$.getJSON(originalUrl + 'getcontent.php', { cid: id }, function(data) {
		if (data) {
			$('div#body_content_content_title').html(data.title).show();
			$('div#body_content_content_body').html(data.body).show();
			$('div#body_content_content').slideDown();
		}
	});
	
	togglePromotionContent(id);
}

function retrieveProductsInCategory(id) {
	id = id.replace('t', '');
	$('div#body_content_content').slideUp();
	$.getJSON(originalUrl + 'getcategory.php', { cid: id }, function(data) {
		if (data) {
			var content = '<table class="product" width="100%" border="0" cellspacing="0" cellpadding="10">';
			for(var i=0; i<data.length; i++) {
				content += '<tr id=\'product' + data[i].pid + '\'>';
				content += '<td width="30%"><img src=\'images/product/' + data[i].thumbnail + '\' /></td>';
				content += '<td class="productdesc"><h3>' + data[i].name + ' &nbsp;&nbsp;[code: ' + data[i].sku + ']' + '</h3>';
				content += data[i].shortdesc;
				if (data[i].price != 0)
				    content += '<h5>ราคา: ' + data[i].price + ' บาท &nbsp;[สมาชิก: ' + getMemberPrice(data[i].price) + ' บาท]</h5>';
				else
				    content += '<h5>ราคา: Please contact us for the price.</h5>';
				content += '</td>';
				content += '</tr>';
			}
			content += '</table>';
			
			$('div#body_content_content_title').html(categoryName[id]).show();
			$('div#body_content_content_body').html(content).show();
			$('div#body_content_content').slideDown();
			
			for(var i=0; i<data.length; i++) {
				var ele = $('div#body_content_content_body table tr#product' + data[i].pid);
				ele.click(function() { retrieveProduct(this.id); });
				ele.hover(
				  function () {
					$(this).addClass("producthover");
				  },
				  function () {
					$(this).removeClass("producthover");
				  }
				);
			}
		}
	});
	
	togglePromotionContent(0);
}

function retrieveProduct(id) {
	id = id.replace('product', '');
	$('div#body_content_content').slideUp();
	$.getJSON(originalUrl + 'getproduct.php', { pid: id }, function(data) {
		if (data) {
			var title = data.name + ' &nbsp;&nbsp;[code: ' + data.sku + ']';
			$('div#body_content_content_title').html(title).show();
			
			var content = '<div><img src=\'images/product/' + data.imageurl + '\' /></div>';
			content += '<h4>ราคา: &nbsp;' + data.price + ' บาท &nbsp;[สมาชิก: ' + getMemberPrice(data.price) + ' บาท]</h4>';
			content += data.desc;
			$('div#body_content_content_body').html(content).show();
			$('div#body_content_content').slideDown();
		}
	});
}
