var id2change = null;
var id2url = null;
var oldhtml = '';
var ref = "";
document.domain = 'kapanlagi.com';
if (document.referrer) {
	ref = document.referrer;
}
$(document).ready(function() {
/*	
	$('.buy-button').each(function() {
		var title = $(this).attr("href");
		var sd = title.replace(/..$/, 'sd');
		var wm = title.replace(/..$/, 'wm');
		var items_menu = [
			{src: 'Format HP', url: sd},
			{src: 'Format PC', url: wm}
		];
		var options_menu = {minWidth: 60, arrowSrc: 'http://www.kapanlagi.com/v4/i/arrow_right.gif'}; //, onClick: function(e,menuItem) { var h = $(this).children('div').children('a'); alert(h.attr('href')); return false;}};
		$(this).menu(options_menu, items_menu);
	});
*/
	var options = {
		target: '#ftd_message',
		beforeSubmit: showRequestLogin,
		success: showResponseLogin
	};
	$('#ftd_form').submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	});
	$('.bluelinkftd').click(function() {
		var myurl = $(this).attr("href");
		var mytarget = $(this).attr("target");
		if (mytarget == "albwin") {
			window.open(myurl,'albwin','left=100,top=30,width=450,height=400,toolbar=1,resizable=0');
		}
		else {
			var options = {
				url: myurl,
				beforeSubmit: showRequest,
				success: showResponse
			};
			if (myurl.indexOf('pos=left') > -1) {
				$('#ftd_form_list_left').ajaxSubmit(options);
			}
			else {
				$('#ftd_form_list').ajaxSubmit(options);
			}
			oldhtml = $(this).html();
			$(this).html('<img src="http://www.kapanlagi.com/v4/i/ajaxloading.gif" alt="" width="16" height="16" />');
			id2change = $(this);
			id2url = myurl;
		}
		return false;
	});
	$('#orderby').change(function() {
		window.location = $(this).val();
	})
});

function showRequest(formData, jqForm, options) {
	return true;
}

function showResponse(responseText, statusText)  {
	if (responseText.indexOf("refresh") > -1) {
		var spl = responseText.split("||");
		//alert(spl[2]);
		window.location = window.location.href;
		id2change.html(oldhtml);
	}
	else {
		var spl = responseText.split("||");
		$("#info_item").html(spl[0]);
		$("#info_price").html(spl[1]);
		$("#info_item2").html(spl[0]);
		$("#info_price2").html(spl[1]);

		$(".info_top").html(spl[2]);
		$(".info_top").removeClass("info_top_hidden");
		$(".info_top").addClass("info_top_display");

		$(".info_top_br").html('<br style="clear:both;"/>');
		$(".info_top_br").removeClass("info_top_hidden");
		$(".info_top_br").addClass("info_top_display");
		window.location = '#mdtop';
		
		id2change.attr("href",spl[4]);
		id2change.html(spl[3]);
		//checkGrey(id2change.attr("href"), spl[3].toLowerCase());
	}
	return true;
}

function checkGrey(link, mod) {
	link = link.replace(/&/g, '&amp;');
	link = link.replace(/\?/g, '\\?');
	link = link.replace(/\./g, '\\.');
	link = link.replace(/\=/g, '\\=');
	$(".ftd-index-box").each(function() {
		var x = $(this).html();
		if (x.match(new RegExp(link,'g'))) {
			var allowed = true;
			if (x.match(/\<a(.+?)\>\<b\>Hapus\<\/b\>\<\/a\>/g)) allowed = false;
			$(this).children('div').each(function() {
				if (mod == "<b>hapus</b>") {
					$(this).addClass('ftd-disabled');
				}
				else {
					$(this).removeClass('ftd-disabled');
				}
				$(this).children('div').each(function() {
					if (mod == "<b>hapus</b>") {
						if ($(this).html() != 'Beli Album' && $(this).html() != 'Preview') $(this).addClass('ftd-disabled');
					}
					else {
						if (allowed === true && $(this).html() != 'Beli Album' && $(this).html() != 'Preview') $(this).removeClass('ftd-disabled');
					}
				});
			});
		}
	});
}

function showRequestLogin(formData, jqForm, options) {
	$('#ftd_message').html('<img src="http://www.kapanlagi.com/v4/i/ajaxloading.gif" alt="" width="16" height="16" /> Silahkan tunggu...');
	return true;
}

function showResponseLogin(responseText, statusText)  {
	var hr = window.location.href;
	var spl = hr.split("?");
	var nl = hr.replace(/(.+?)\?ref\=/,'');
	if (!hr.match(/\?ref\=/)) nl = ref;
	if(responseText == 'Login sukses') {
		if (nl != "") {
			window.location=nl;
		}
		else {
			window.location='home.php';
		}
	}
	return true;
}

function checkAll(toggler) {
	$(".cart-checkbox").each(function() {
		if (toggler)
			$(this).attr("checked", "checked");
		else
			$(this).removeAttr("checked");
	});
}

function deleteChecked() {
	$(".cart-checkbox:checked").each(function() {
		var d = $(this).val();
		var sp = d.split("||");
		$.post(
		      "/service.php",
		      "method=del&id="+sp[0]+"&media="+sp[1],
		      function(data, textStatus){
			$("#tr"+sp[1]+sp[0]).fadeOut("slow", function() {$(this).remove();});
			var spl = data.split("||");
			var totIvas = spl[6];
			var totSms = spl[5];
			$("#subtotal_ivas_rp").html(totIvas);
			$("#subtotal_sms_rp").html(totSms);
			totIvas = totIvas.replace(/[^0-9]/, '');
			var ppnIvas = parseInt(totIvas) * 0.1;
			totIvas = parseInt(totIvas) + ppnIvas;
			totSms = totSms.replace(/[^0-9]/, '');
			var ppnSms = parseInt(totSms) * 0.1;
			totSms = parseInt(totSms) + ppnSms;
			$("#ppn_ivas_rp").html(getNumberFormat(ppnIvas));
			$("#ppn_sms_rp").html(getNumberFormat(ppnSms));
			$("#total_ivas_rp").html(getNumberFormat(totIvas));
			$("#total_sms_rp").html(getNumberFormat(totSms));
			if (totSms == 0) window.location = "/cart2.php";
		      }
		);
	})
}

function getNumberFormat(x) {
	x = ''+x;
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x)) {
		x = x.replace(rgx, '$1' + '.' + '$2');
	}
	return x;
}

function submitsearch(formname) {
	$("#"+formname).submit();
}

function checkrefpage(gift) {
	if (!gift) gift = '';
	var cmember = getCookieValue("mbralname");
	if (cmember == null) {
		$.get('cart2.php','ar=x&arv=frm',function(ct, st) {
			$('#ajaxlogin').html(ct);
			submit_login_form();
			ref = 'http://musicstore.kapanlagi.com'+gift+'/cart2.php?py=1';
		});
		$(".info_top").each(function() {
			$(this).removeAttr("style");
			$(this).addClass("info_top_hidden");
		});
		$("#box_shopping_cart").slideUp("fast");
		return false;
	}
	
	return true;
}

function submit_login_form() {
	var options = {
		target: '#ftd_message',
		beforeSubmit: showRequestLogin,
		success: showResponseLogin
	};
	$('#ftd_form').submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	});
}

/* iklan ipod */
function toggle_ipod() {

	if ($("#ipodimg").is(':hidden')) {
		$("#ipodimg").show("slow");
		$("#ipodimg").removeAttr('style');
	}
	else {
		$("#ipodimg").hide("slow");
	}
}