//hot link drop down
$(document).ready(function() {
	$('.btn-go').click(function() {
		//alert($('.hot-link').val());
		window.location.href=$('.hot-link').val();
	});
});

//check price
$(document).ready(function() {
	$('#postPrice').blur(function() {
		if(isNumeric($('#postPrice').val()) != true) {
			alert('กรุณากรอกเฉพาะตัวเลข');
			//focus
			$('#postPrice').val('');
			$('#postPrice').focus();
		}
	});
});

//fancybox
$(document).ready(function() { 
	$(".fancyBox").fancybox({ 
		'overlayShow': true,
		'hideOnContentClick': true,
		'frameWidth': 500,
		'frameHeight': 400,
		'overlayOpacity': 0.5
	}); 
}); 

//top banner fade
$(document).ready( function(){ 
	$('#topBanner').innerfade({ 
		animationtype: 'fade', 
		speed: 'slow',
		timeout: 6000
	});
});

//thumbnail remove link
$(document).ready(function(){
		//add hand cursor and title
		$('#thumbnails> *').attr('title', 'ลบรูปนี้ออก');
		$('#thumbnails> *').css('cursor', 'pointer');

		//click to remove
		$('#thumbnails> *').click(function() {
			var clickedImg = $(this);
			$.get('remove-thumbs.php', {arg: clickedImg.attr('src')}, function(){
				clickedImg.fadeOut("slow");
				var stats = swfu.getStats();
				var successUploaded = stats.successful_uploads
				//alert(stats.successful_uploads);
				if(successUploaded>0) successUploaded--;
				stats.successful_uploads = successUploaded;
				swfu.setStats(stats);
			});
		});
});

//part dropdown
$(document).ready(function(){
	$('.partSmtBtn').click(function(){
		//alert($('.partCat').val());
		window.location.href='/parts/' + $('.partCat').val();
	});
});

//search
$(document).ready(function(){
	$('.search-input').keypress(function (e) {
		if (e.which == 13) {
			window.location.href='/search/?q=' + urlencode($('.search-input').val());
		}
	});
});


function login(){
	var	username	=document.form_login.username.value;
	var	password	=document.form_login.password.value;
	var	url_load	="/member/login.php";

	var	errMsg		="";
	

	if (password ==""){
			errMsg = "กรุณาระบุ password";
			setfocus = "password";
	}

	if (username ==""){
			errMsg = "กรุณาระบุ username";
			setfocus = "username";
	}

	if (errMsg != ""){
		alert(errMsg);
		eval("document.form_login." + setfocus + ".focus()");
	}else{

		$("#loading_process").fadeIn(300);
		$.post(url_load, "username="+username+"&password="+password, function(data){
				if (data =="OK")
				{
					window.location.reload();
				}else{
					document.form_login.username.value="";
					document.form_login.password.value="";
					$("#loading_process").fadeOut(600);
					alert("Login ผิดพลาดชื่อผู้ใช้หรือรหัสผ่านผิดพลาดกรุณาตรวจสอบอีกครั้ง");	
				}
		});

	}

}
function forgetpassword(){
	var	username	=document.form_forget_password.username.value;
	var	url_load	="/member/forgetpassword.php";

	var	errMsg		="";

	if (username ==""){
			errMsg = "กรุณาระบุ username หรือ email";
			setfocus = "username";
	}

	if (errMsg != ""){
		alert(errMsg);
		eval("document.form_forget_password." + setfocus + ".focus()");
	}else{

		$("#loading_process").fadeIn(300);
		$.post(url_load, "username="+username, function(data){
				if (data =="ERROR")
				{
					document.form_forget_password.username.value="";
					$("#loading_process").fadeOut(600);
					alert("ข้อมูล username หรือ email ผิดพลาดกรุณาตรวจสอบอีกครั้ง");	
				}else{
					$("#loading_process").fadeOut(600);
					$("#profile_login").html(data);
				}
		});

	}


}


function openmsn(){
	document.getElementById("msndisplay").innerHTML="<iframe src=\"http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=92b17e19140de387@apps.messenger.live.com&mkt=th-TH\" width=\"300\" height=\"400\" style=\"border: solid 1px black; width: 300px; height: 400px;\" frameborder=\"0\"></iframe>";
	document.getElementById("msnblock").innerHTML="<a href=\"javascript:closemsn();\" class=\"linkorange\">ปิดหน้าต่างพูดคุย</a>";
	display("msndisplay");
}
function closemsn(){
	document.getElementById("msndisplay").innerHTML="";
	document.getElementById("msnblock").innerHTML="<a href=\"javascript:openmsn();\" class=\"linkorange\" title=\"พูดคุยกับทีมงานได้ทันทีมีทีมงานคอนตอบคำถามอยู่ครับ\">คุยกับเด็กหน้าร้าน KreeBay.com</a>";
	hide("msndisplay");
}


function display(sID) {
	oObj = document.getElementById(sID);
	if (oObj) {
		oObj.style.display='inline';
	} 
}

function hide(sID) {
	oObj = document.getElementById(sID);
	if (oObj) {
		oObj.style.display='none';
	}
}


function loginform(){
	var	url_load	="/member/login.php";

	$("#loading_process").fadeIn(300);

	$.get(url_load, "", function(data){
		$("#loading_process").fadeOut(600);
		$("#profile_login").html(data);
	});
}

function forgetpasswordform(){

	var	url_load	="/member/forgetpassword.php";

	$("#loading_process").fadeIn(300);

	$.get(url_load, "", function(data){
		$("#loading_process").fadeOut(600);
		$("#profile_login").html(data);
	});
}


function cartupdate(){

	var	url_load	="/cart/cartupdate.php";

	$("#loading_cart").fadeIn(100);

	$.get(url_load, "", function(data){
		$("#loading_cart").fadeOut(100);
		$("#widget_cart").html(data);
	});

}


function cartdelete(id){

	var	url_load	="/cart/cartdelete.php";

	$("#loading_cart").fadeIn(100);

	$.get(url_load, "id="+id, function(data){
		$("#loading_cart").fadeOut(1000);
		$("#widget_cart").html(data);
	});

}


function itemdelete(){


	var	url_load		="/cart/itemdelete.php";
	var file_del		="";
	var item_id			="";
	var i				="";

//	if (window.confirm("ยืนยันการลบอีกครั้ง ?")) {

		if (document.form_item.item_id.length !=undefined)
		{


			for (i=0; i<=document.form_item.item_id.length-1; i++){
					if (document.form_item.item_id[i].checked)
					{

						file_del = document.form_item.item_id[i].value;

						if (file_del !="" && file_del != null){
								item_id =item_id+"&id"+i+"="+file_del;
						}
					}
			}
			item_id =item_id+"&count="+i+"&p=1";

/**/
		}else{

	if (document.form_item.item_id.checked==true){
					file_del = document.form_item.item_id.value;
						if (file_del !="" && file_del != null){
								item_id =item_id+"&id0="+file_del;
						}
				}
			item_id =item_id+"&count=1&p=1";
/**/
		}


		if (item_id !=""){
			$("#loading_item").fadeIn(300);
			$.get(url_load, item_id, function(data){
				$("#loading_item").fadeOut(1000);
				$("#itemhtml").html(data);
			});
		}

	}

//}


function itemupdateup(id){


	var	url_load		="/cart/itemupdateup.php";

			$("#loading_item").fadeIn(300);
			$.get(url_load, "id="+id, function(data){
				$("#loading_item").fadeOut(1000);
				$("#itemhtml").html(data);
			});

}


function itemupdatedown(id){


	var	url_load		="/cart/itemupdatedown.php";

			$("#loading_item").fadeIn(300);
			$.get(url_load, "id="+id, function(data){
				$("#loading_item").fadeOut(1000);
				$("#itemhtml").html(data);
			});

}

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var histogram = {}, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function isNumeric(num){
	if (num >=0 || num < 0) return true;

	return false;
} 