// JavaScript Document

function fn_kredi_karti_check_exceptions(id){
	if (pCont !='checkout' || pCont !='orders'){
		$.taksit.setPrice($.taksit.getPrice(id));
	}
}
function fn_switch_card(id){
	$('#taksit').getTablo(id);
}
function pFloat(val){
	var val = val || 0;
	val = val.toString();
	if (val.indexOf('.') != -1) {
		if (val.substr(val.indexOf('.') +1,3) > 995){
			val = Math.round(val);	
		}
	}
	return eval(val);
}
function pSize(data){
	var x = 0
	for(i in data){x++;}
	return x;
}
function Tablo(config){
	this.maxRow  =  config.maxRow || 0;
	this.table   = {};
	this.tId	 = config.tId || false;
	this.id		 = config.id;
	this.color	 = config.color || '#f7f7f7';
	return this.init();
}

Tablo.prototype = {
	init: function(){
		this.table = $('<table cellpadding="0" border="1" cellspacing="1" id="'+this.id+'"></table>');
		if (this.tId){
			if (this.id=='tInfo'){
				this.table.append('<tr><td><span class="tInfo-first"></span></td></tr><tr><td>Taksit (Ay)</td></tr>');
			}else{
				this.table.append('<tr><td colspan="2"><img src="/addons/kredi_karti/images/'+this.id+'.gif" border="0"></td></tr><tr><td>Taksit</td><td>Toplam</td></tr>');
			}			
			for (x in pData[this.tId].taksit){
				this.addRow(pData[this.tId].taksit[x].adet);		
			}
			$("tr:gt(1):even",this.table).css("background-color", this.color);
			$("tr:lt(2) td",this.table).css({"text-align":"center","padding":"0"});
		} else {
			if (this.id=='tInfo'){
				this.table.append('<tr><td><span class="tInfo-first"></span></td></tr><tr><td>Tek Çekim</td></tr><tr><td>Taksit (Ay)</td></tr>');
			}else{
				this.table.append('<tr><td colspan="2"><img src="/addons/kredi_karti/images/'+this.id+'.gif" border="0"></td></tr><tr id="1"><td colspan="2" class="price">&nbsp;</td></tr><tr><td>Taksit</td><td>Toplam</td></tr>');
			}
			for (x=2 ; x <= this.maxRow; x++){		
				this.addRow(x);
			}	
			$("tr:gt(2):even",this.table).css("background-color", this.color);
			$("tr:lt(3) td",this.table).css({"text-align":"center","padding":"0"});
		}
		return this.table;
	},
	addRow : function(id){
		if (this.id=='tInfo'){
			if (this.tId){
				this.table.append('<tr id="'+id+'"><td><span><input name="payment_info[taksit]" type="radio" value="'+id+'" /></span>'+id+' Taksit</td></tr>');
			}else{
				this.table.append('<tr><td>'+id+' Taksit</td></tr>');
			}
		}else {
			this.table.append('<tr id="'+id+'"><td>&nbsp;</td><td>&nbsp;</td></tr>');
		}
	}
};

(function($) {
	var container,tId,decplaces;
	$.fn.extend({
		getTablo:function (tableId){
			container  	= this;
			tId 		= (tableId)?tableId:false;
			$.taksit.init();
			decplaces = currencies.primary.decimals;
		}
	});
	$.taksit = {
		init : function (){
			container.hide();
			maxRow =0;
			if (tId) {
				if (tId != 'base' && pSize(pData[tId].taksit) > 0){
					var color = pData[tId].color;
					container.html('');
					container.append(new Tablo({id:'tInfo',tId:tId}));
					container.append(new Tablo({id:tId,color:color,tId:tId}));
					container.append('<span class="clear-both"></span>');
					container.show();
					this.setPrice(this.getPrice());
				}else{
					tId = pBase['Pos'];
					this.setExceptions(this.getPrice());
				}
				if (pData[tId]['3dsecure'] == 'C'){
					$('.tree-d-box').show();
					$('.tree-d-box input').attr('disabled','');
				}else {
					$('.tree-d-box').hide();
					$('.tree-d-box input').attr('disabled','disabled');
				}
			} else {
				maxRow = this.getMaxRow();
				pWidth = container.parents('.cm-tabs-content').eq(0).width();
				tWidth = 0;
				for(i in pData){
					var color = pData[i].color;
					if ((pWidth-tWidth) < 150){tWidth = 0;}
					if (tWidth == 0){
						container.append(new Tablo({id:'tInfo',maxRow:maxRow}));
						tWidth +=80;
					}
					container.append(new Tablo({id:i,maxRow:maxRow,color:color}));	
					tWidth +=150; 
				}
				container.append('<span class="clear-both"></span>');
				this.setPrice(this.getPrice());
				container.show();
			}
		},
		getMaxRow: function (){
			var maxRow = 0;
			for(x in pData){
				for(y in pData[x].taksit){
					maxRow =(maxRow<parseInt(pData[x].taksit[y].adet))?parseInt(pData[x].taksit[y].adet):maxRow;
				}
			}
			return maxRow;
		},
		setPrice : function (pPrice){
			//alert(pPrice);
			if (parseFloat(pPrice) > 0){
				if (pCont=='checkout' || pCont =='orders'){
					if (!tId){
						tId = pBase['Pos'];	
					}
					this.setPriceTable(pPrice,tId);
					this.setExceptions(pPrice);
				}else{
					for (x in pData){
						this.setPriceTable(pPrice,x);
					}
					this.setExceptions(pPrice);
				}
			}
		},
		setExceptions:function(pPrice){
			pPrice = eval(pPrice);
			var surcharge  = parseFloat(pBase.A);
			surcharge += pPrice*(parseFloat(pBase.P)/100);
			var tPrice = $.formatNum(pFloat(pPrice+surcharge), decplaces, true);
			if (pCont=='checkout' || pCont =='orders'){
				if (tId !='base'){
					surcharge  = parseFloat(pData[tId].a_surcharge);
					surcharge += pPrice*(parseFloat(pData[tId].p_surcharge)/100);
					tPrice = $.formatNum(pFloat(pPrice+surcharge), decplaces, true);
				}
				$('input',$('.tek-cekim')).attr('checked','checked');

				$('#sec_tek_cekim').html(tPrice);
				$('.tek-cekim input:first').attr({'S':surcharge,'T':tPrice}).click(function(){$.taksit.setTotal($(this).attr('S'),$(this).attr('T'));});
				this.setTotal(surcharge,tPrice);
			}else{
				$('#tek_cekim').html(pTpl.replace('0'+currencies.primary.decimals_separator+'00',tPrice));
			}
		},
		setTotal : function (surcharge,tPrice){
			// Toplam tablosu  - Surcharge
			_sur = $.formatNum(pFloat(surcharge), decplaces, true)
			var ul = $('.statistic-list');
			var nSur = $('#payment_surcharge_line',ul);
			if (nSur.html()){
				if (surcharge <= 0){
					nSur.remove();
				} else {
					$('#sec_payment_surcharge_value',ul).html(_sur);
				}
			} else {
				if (surcharge > 0){
					$('.total',ul).before('<li id="payment_surcharge_line"><span>'+pSur+':</span><strong>'+tTpl.replace('0'+currencies.primary.decimals_separator+'00',_sur)+'</strong></li>');
				}
			}
			// Toplam Tablosu Genel Toplam
			$('#sec_cart_total').html(tPrice);	
		},
		setPriceTable :function(pPrice,pId){
			pPrice = eval(pPrice);
			pId = (pId)?pId:tId;
			var tInfo =$('#tInfo');
			for (y in pData[pId].taksit){
				var p =pData[pId].taksit[y]; 
				if (p.tur == 'P') {
					var fark = pPrice*(this.toFloat(p.miktar)/100);
				} else {
					var fark = this.toFloat(p.miktar);
				}
				fark = pFloat(fark);
				var elm = $('#'+p.adet,$('#'+pId,container));
				$('td:first',elm).html(pTpl.replace('0'+currencies.primary.decimals_separator+'00',$.formatNum(pFloat((fark+pPrice)/p.adet), decplaces, true)));
				$('td:last' ,elm).html(pTpl.replace('0'+currencies.primary.decimals_separator+'00',$.formatNum(pFloat(fark+pPrice), decplaces, true)));
				$('#'+p.adet+' input',tInfo).attr({'S':fark,'T':$.formatNum(pFloat(fark+pPrice), decplaces, true)}).click(function(){$.taksit.setTotal($(this).attr('S'),$(this).attr('T'));});
			}
			var A = parseFloat(pData[pId].a_surcharge);
			var P = pPrice*(parseFloat(pData[pId].p_surcharge)/100);
			$('#1 td',$('#'+pId,container)).html(pTpl.replace('0'+currencies.primary.decimals_separator+'00',$.formatNum((A+P+pPrice), decplaces, true)));	
		},
		getPrice : function (id){
			if (id){
				if((typeof(update_ids[id['id']]) != 'undefined') ){
					return (typeof(update_ids[id['id']]['product_price']['P'])!= 'undefined')?update_ids[id['id']]['product_price']['P']:update_ids[id['id']]['original_price']['P'];

				}else {
					return parseFloat(updated_price[id['id']]);
				}
			} else {
				if (pCont =='checkout' || pCont =='orders'){
					return pPrice;
				} else {
					var PBox = $('form[name^="product_form_"]');
					var P = $('#sec_cart_total',PBox).eq(0);
					if (P.html()){
						return this.toFloat(P.html());
					}
					var pId=this.getPid();
					var K = $('#sec_product_price_'+pId,PBox).eq(0);
					if (K.html()){
						return this.toFloat(K.html());
					}				
					var D = $('#discounted_price_'+pId,PBox).eq(0);
					if (D.html()){
						return this.toFloat(D.html());
					}
					var S = $('#sec_discounted_price_'+pId,PBox).eq(0);
					if (S.html()){
						return this.toFloat(S.html());
					}
				}
			}
			return false;			
		},
		toFloat : function (tmp){
			tmp = tmp.replace(currencies.primary.thousands_separator,'');
			tmp = tmp.replace(currencies.primary.decimals_separator,'.');
			return pFloat(tmp);
		},
		getPid : function (){
			var pFrm = $("form[name^='product_form_']");
			if (pFrm.attr('name')){
				return pFrm.attr('name').replace('product_form_','');
			}
			return 0;
		}
	};
})(jQuery);
