/* Real gifts new */
/*
Rgifts = {
	soap: 0,
	_init: function() {
		if ($("div.ziepes_bumbas").length > 0) {
			Rgifts.soap_subst_toggle();
		}
	},
	/* SOAP subproduct selection /
	soap_subst_toggle: function(){
		var soap = 1;
		var hide = false;
		$("div.ziepes_bumbas").each(
			function(){
				//alert($("#soap_subst" + soap + " input:checked").attr("id"));
				if (typeof $("#soap_subst" + soap + " input:checked").attr("id") == "undefined" && hide == false) {
					hide = true;
					Rgifts.soap_subst_display(soap, "show");
				} else {
					Rgifts.soap_subst_display(soap, "hide");
				}
				soap++;
			}
		);
	},
	soap_subst_display: function(subst_nr, type) {
		$("#soap_subst" + subst_nr + " .zb_radio_select").each(
			function(){
				if (type == "show") {
					$(this).show();
				} else {
					if ($("#" + $(this).attr("id") + " input").attr("checked") == false) {
						if (type == "toggle") {
							$(this).toggle();
						}
						if (type == "hide") {
							$(this).hide();
						}
						
					}
				}
			}
		);
		
	},
}
$(function() {
	Rgifts._init();
});
*/
/* ################ */
function gift_tabs(tab) {
	$('#tab_box1').hide();
	$('#tab_box2').hide();
	$('#tab_box3').hide();
	$('#tab_box'+tab).show();
	rq('/gifts2/rq/tab'+tab+'.php','tab_box'+tab);
	return false;
}
function sendGift(giftId, step, fid) {
	$("#tab_box2").hide();
	$("#tab_box3").hide();
	$("#tab_box1").show();
	if (giftId > 0) {
		rq('/gifts2/rq/tab1_send.php?gid='+giftId+'&step='+step+'&fid='+fid,'tab_box1');
	} else {
		rq('/gifts2/rq/tab1.php?fid='+fid,'tab_box1');
	}
	scroll(0,0);
	try {
		InfoBox.close();
	}
	catch(err) {
	}
	return false;
}
function delGift(box, id, pg, text) {
	if (confirm(text)) {
		rq('/gifts2/rq/tab'+box+'.php?del'+(box-1)+'='+id+'&pg='+pg,'tab_box'+box);
		scroll(0,0);
		return false;
	}
	return false;
}
function recoverGift(box, gid, pg) {
	rq('/gifts2/rq/tab'+box+'.php?recover='+gid+'&pg='+pg,'tab_box'+box);
	return false;
}
function removeGift(box, gid, pg) {
	rq('/gifts2/rq/tab'+box+'.php?remove='+gid+'&pg='+pg,'tab_box'+box);
	return false;
}
function giftType(gid,fid) {
	$('#send_i').hide();
	$('#send_i2').show();
	if (fid == 0) {
		fid = document.getElementById('fid_sel').options[document.getElementById('fid_sel').selectedIndex].value;
	}
	rq('/gifts2/rq/type_list.php?gid='+gid+'&fid='+fid,'send_i2');
	return false;
}
function giftSubmit(alert_text) {	
	var checked = false;
	$("input[type=radio]").each( function () {
		if ($(this).attr("checked") == true) {
			checked = true;
		}
	});
	if (!checked) {
		alert(alert_text);
	}
	return checked;
}
function limitText(textArea, length) {
    var ll = textArea.value.length;
	if (ll > length) {
        textArea.value = textArea.value.substr(0,length);
		ll = length;
    }
    document.getElementById('count').value = (length-ll);
}

/* NEW */
/*
function gift_send(gid,fid) { 
	if (!gid) { 
		rq('/gifts/rq/sms_all_gifts.php?fid='+fid,'gifts_ct');
	} else { 
		rq('/gifts/rq/sms_send_gift.php?gid='+gid+'&fid='+fid,'gifts_ct');
	}
}
*/

function gift_control(action, box, gid, pg) { 
	if (action==1) { // recover firstpage
		rq('/gifts/rq/sms_mygifts.php?box='+box+'&recover='+gid+'&pg='+pg,'gifts_ct');
	} else if (action==2) { // remove firstpage
		rq('/gifts/rq/sms_mygifts.php?box='+box+'&remove='+gid+'&pg='+pg,'gifts_ct');
	}
}

function calendar_user_info(uid) { 
	$("#calendar_box").hide();
	$("#show_calendar").show();
	$("#user_info").show();
	rq('/gifts/rq/calendar_summary_user.php?uid='+uid,'user_info');
}

/* real */
var prev_cat = 0;
function pages(file,cat,scat,pid,fid,pg) {
	$(".rsmenu").blur();
	query = '';
	if (file == 'real_cat_list') {
		target = 'gifts_ct';
		query = 'gmenu=2&cat='+cat+'&scat='+scat+'&pid='+pid+'&fid='+fid+'&pg='+pg;
	} 
	if (file == 'real_prod_view') {
		target = 'rgift_product';
		pridir = 'pg';
		if (fid == 1) {
			pridir = 'navprev';
		}
		if (fid == 2) {
			pridir = 'navnext';
		}
		if (pid > 0) {
			rg_tshirt_id = 0;
			rg_cup_id = 0;
		}
		query = 'gmenu=2&cat='+cat+'&scat='+scat+'&pid='+pid+'&'+pridir+'='+pg;
	}
	if (file == 'real_special_offer') {
		target = 'gifts_ct';
		query = 'gmenu=10';
	}
	if (file == 'search') {
		target = 'gifts_ct';
		query = 'gmenu=15&search_price='+cat+'&pg='+pg;
		cat = 0;
		scat = 0;
		pid = 0;
		fid = 0;
	}
	$(".rsmenu").removeClass("show");
	if (cat != prev_cat || prev_cat == 0) {
		if ($("#rsmenu"+prev_cat)) {
			$("#rsmenu"+prev_cat).hide("fast");
			$("#rsmenu"+cat).slideDown("slow");
		} else {
			$("#rsmenu"+cat).slideDown("slow");
		}
	} else {
		if (cat == prev_cat) {
			if (!scat) {
				$("#rsmenu"+cat).toggle("slow");
			} else {
				$("#rsmenu"+cat).show();
			}
		}
		
	}
	$("#rmenu > li > a").removeClass("sel");
	$("#rmenua"+cat).addClass("sel");
	if (cat != 10) $("#rmenua10").addClass("red");
    if (cat != 11) $("#rmenua11").addClass("red");
    if (cat != -10) $("#rmenua-10").addClass("red");
    if (cat != -1) $("#rmenua-1").addClass("red");
    if (cat != -2) $("#rmenua-2").addClass("red");
    if (cat != -3) $("#rmenua-3").addClass("red");
	if (scat) {
		$(".rsmenu > li > a").removeClass("sel");
		$("#rsmenua"+scat).addClass("sel");
	} else {
		
	}
	prev_cat = cat;
	rq('/rgifts/rq/'+file+'.php?'+query,target);
}
function spec(divID,stop) {
	if (stop) {
		if (startpageRnd) {
			clearTimeout(startpageRnd);
		}
		$("#rgift_top2_nr"+divID).blur();
	}
	$(".rgift_top2").addClass("rgt2hide");
	$("#top2_"+divID).removeClass("rgt2hide");
	$(".rgift_top2_nr > div").attr("class","wt");
	$("#rgift_top2_nr"+divID).attr("class","sel");
}
function startpageRndButtons(i) {
	
	var bt_cnt = $(".rgift_top2_nr > div").length;
	if (bt_cnt > 0) {
	if (i >= bt_cnt) {
		i = 0;
	}
	spec(i);
	i = i + 1;
	startpageRnd=setTimeout("startpageRndButtons("+i+")",3000);
	} else {
		
	}
}
function showSmsAct(a){if ($("div.box4four:visible").length > 1) {for(i=0;i<$("div.box4four:visible:first").css("height");i++){$("div.box4four:visible:first").css("margin-top",(0-i));}$("div.box4four:visible:first").hide();}else{$("div.box4four").css("display","block","margin-top","0");}startpageRnd=setTimeout("showSmsAct(0)",5000);}

function stopNrButtons() {
	clearTimeout(startpageRnd);
	$(this).unbind("mouseover");
}
function updateSummaryPrice(price) {
	if (price != RG_delivery_price) {
		price = RG_delivery_price;
	}
	$("td.count > input:not(value:'')").each(function() {
		price = price+(parseInt($(this).attr("rel"))*parseInt($(this).attr("value")));
		//alert((parseInt($(this).attr("rel"))*parseInt($(this).attr("value"))));
	});
	
	old_html = $("#summary_price").html().replace(new RegExp("^[0-9.0-9/e+/]+", "g"),"");
	if (price > 0) {
		if (price >= 11800) {
			$("#percode").show();
		} else {
			$("#percode").hide();
		}
		price = new String(price);
		price = price.substr(0,price.length-2)+'.'+price.substr(price.length-2,2);
		$("#summary_price").html(price+old_html);
		$("#summary_price2").html(price+old_html);
	} else {
		$("#summary_price").html("0.00"+old_html);
		$("#summary_price2").html("0.00"+old_html);
	}
}
function changeImage(pid, npk) {
	
	if (typeof($("#prod_images"+npk)) != "undefined") {
		npk = npk;
	} else {
		npk = 0;
	}
	if (npk == 0) {
		bgurl = $("#rgift_prod_images img").attr("src").replace("_m_","_l_").replace("_1_","_l_").replace("_2_","_l_");
	} else {
		bgurl = $("#prod_images"+npk).css("background-image");
		bgurl = bgurl.replace("url(","").replace(")","").replace("_s_","_l_").replace("_e_","_l_").replace("_1_","_l_").replace("_2_","_l_").replace('"',"").replace('"',"");
	}
	//$("#rgift_prod_images img").attr("src",bgurl);
	//$("#prod_images"+npk).css("background-image","url("+src+")");
	if($('.jqmOverlay').size()){
		InfoLite.close();
	 }
	$(".jqmWindow").css("background","none");
	$(".jqmWindow").css("border","none");
	//InfoLite.open({text:'<img id="imgL'+npk+'" src="'+bgurl+'" onclick="InfoLite.close();" />', height: 600, width: 800});
	InfoLite.open({text:'<div class="imgL" style="background: url('+bgurl+') no-repeat 50% 50%;" onclick="InfoLite.close();">&nbsp;</div>', height: 600, width: 800});
	//$("#infolite").click(function(){InfoLite.close()});
}
function addWishlistRG(prod_id) {
	$.get("/rgifts/rq/add_wishlist.php", { type: 2, pid: prod_id },
		function(data){
		InfoLite.open({text:data, width: 400});
	});
	setTimeout("InfoLite.close()",3000);
	return false;
}
function chDel(delt,maxdt) {
	city = 0;
	if (delt == 1) {
	
		RG_last_price = RG_delivery_price;
		RG_delivery_price = 0;
		//$("#no_delivery").hide();
		resetDelCity(0,0);
		$(".delivery_type_bx").slideUp('fast').ready(function(){
			$("#delivery_1_bx").slideDown();
		});
		
		
	} else {
		$(".delivery_type_bx").slideUp('fast').ready(function(){
			$("#delivery_"+delt+"_bx").slideDown();
		});
		
		
		//$("#no_delivery").show();
		chDelPrice(RG_last_city, RG_last_price);
		
	}
	
}
function deliveryCal(pid, seldate, delt) {
	if($('.jqmOverlay').size()){
		InfoBox.close();
	}
	InfoBox.open("rq/real_delivery_cal.php?pid="+pid+"&seldate="+seldate+'&delt='+delt,{width: 130, height: 130});
}
function deliveryCities(country,distr_code, teritory_code, code, search) {
	
	if (typeof(distr_code) == "undefined") {
		distr_code = 0;
	} else {
		distr_code = parseInt(distr_code);
	}
	if (typeof(teritory_code) == "undefined") {
		teritory_code = 0;
	} else {
		teritory_code = parseInt(teritory_code);
	}
	if (typeof(code) == "undefined") {
		code = 0;
	} else {
		code = parseInt(code);
		if (typeof(rg_dcity_text[code]) != "undefined") {
			$("#city_text").attr("value",rg_dcity_text[code]);
			//$("#city[2]").attr("value",code);
			$("#city_3").attr("value",code);
			//alert(code);
			//$("#city[5]").attr("value",code);
			fillDelCity();
		}
		
	}
	if (typeof(search) == "undefined") {
		search = 0;
	} else {
		search = parseInt(search);
	}
	if($('#infobox3_box').size()){
		InfoBox.close();
	}
	if (code == 0) {
		InfoBox.open("/rgifts/rq/real_delivery_cities.php?pcat="+rg_delivery_pcat+"&cntry="+country+"&dcode="+distr_code+"&tcode="+teritory_code+"&code="+code+"&s="+search,{width: 800, height: 600});
	} 
	return false;

}
var selTime = new Array();
selTime[0]="10:00 - 13:00";
selTime[1]="13:00 - 16:00";
selTime[2]="16:00 - 19:00";
selTimeVal = new Array();
selTimeVal[0]=10;
selTimeVal[1]=13;
selTimeVal[2]=16;
function selDate(date,times,delt) {
	if (date && times) {
		i=0;
		inp_key = delt + 1;
		$("input[name='date["+inp_key+"]']").attr("value",date);
		var sel = true;
		$("select[name='time'] > option").each(function() {
			if (times.substr(i,1)=="1") {
				$(this).attr("value",selTimeVal[i]);
				time_opt = (delt == 1 ? selTime[i].replace("10","11").replace("19","18") : selTime[i]);
                $(this).html(selTime[i]);
				if (sel) {
					$(this).attr("selected","selected");
					sel = false;
				} else {
					$(this).attr("selected","");
				}
			} else {
				$(this).attr("value","");
				$(this).html("");
			}
			i=i+1;
		});
	}
	InfoBox.close();

}
function searchGifts() {
	price = $("#search_price").options[$("#search_price").selectedIndex].value;
	rq('/rgifts/rq/search.php?gmenu=15&search_price='+price,'gifts_ct');
}
var RG_delivery_price = 0;
var RG_last_city = 0;
var RG_last_price = 0;
var rgift_dcpl = new Array();
function chDelPrice_old(city, price, d_pcat) {
	if (typeof(d_pcat) == "undefined") {
		d_pcat = 0;
    } else {
        rg_delivery_pcat = new Number(d_pcat);
    }
    if (city > 0) {
		if (rgift_dcpl[city] == undefined) {
		} else {
			price = rgift_dcpl[city];
			RG_delivery_price = new Number(price);
			price = new Number(price);
		}
		RG_last_city = city;
	}
    if (typeof(price) == "undefined" || price == 0) {
        $.get("/gifts/js/prices.php?"+rg_delivery_pcat+city, function(data){
                price = data;
        });
    }
	if (price > 0) {
		RG_delivery_price = new Number(price);
		price = new Number(price);
	} else {
		price = 0;
		RG_delivery_price = 0;
	}
	
	updateSummaryPrice(price);
	if ($("#delprice").html()) {
		old_html = $("#delprice").html().replace(new RegExp("^[0-9.0-9/e+/]+", "g"),"");
	} else {
		old_html = " Ls";
	}
	if (price > 0) {
		price = new String(price);
		price = price.substr(0,price.length-2)+'.'+price.substr(price.length-2,2);
		$("#delprice").html(price+old_html);
		$("#delprice2").html(price+old_html);
	} else {
		$("#delprice").html("0.00"+old_html);
		$("#delprice2").html("0.00"+old_html);
	}
}
function resetDelCity() {
	$("#city_2").attr("value","0");
	$("#city_3").attr("value","0");
	$("#scity_text1").html("");
	$("#city_text").attr("value","");
	$("#scity_text").hide();
	$("#city_text").show();
	chDelPrice();
}
function fillDelCity(obj, event) {
	
	var city = $("#city_3").attr("value");
	$("#city_text").blur();
	//if (city > 0) {
		//$("#tat_table").hide();
		rg_city_text = $("#city_text").attr("value");
		$("#city_text").hide();
		$("#scity_text").show();
		$("#scity_text1").html(rg_city_text);
		var rg_delivery_city_price = 0;
		$.get("/gifts/js/prices.php?"+rg_delivery_pcat+city, function(data){
			chDelPrice(city, data, rg_delivery_pcat);
		});
	//}
	return true;
}
function addSubProd(prod_id,prod_scid) {
	if (prod_id > 0 && prod_scid > 0) {
		$("form#rgift_prod").append('<input type="hidden" name="spid['+prod_id+']" /><input type="hidden" name="spidsc['+prod_id+']" value="'+prod_scid+'" />');
		$("form#rgift_prod").submit();
	}
}
function switchTshirts(t_id, link_obj, t_side) {
	if (typeof(t_id) != "undefined" && t_id > 0) {
		rg_tshirt_id = new Number(t_id);
		//rg_tshirt_print = new Number(t_id.substring(1,1));
		rg_tshirt_print = new String(rg_tshirt_id);
		rg_tshirt_print = rg_tshirt_print.substring(2,1);
	}
	if (typeof(t_side) != "undefined" && t_side > 0) {
		rg_tshirt_side = t_side;
		$("#rgift_prod_images td a").removeClass("selected");
	}
	//if (typeof(rg_tshirt_side) == "undefined") {
		//rg_tshirt_side = 2;
	//}
	if (rg_tshirt_id > 0) {
		$("#bg_print").css("background-image","url('http://timg.draugiem.lv/img/gifts/tshirts/tshirt_"+rg_tshirt_id+"_"+rg_tshirt_side+".jpg')");
		if (typeof($("#prod_print").attr("src")) != "undefined") {
			$("#prod_print").attr("src",($("#prod_print").attr("src").replace("_"+(3-rg_tshirt_print)+"_","_"+rg_tshirt_print+"_")));
		}
		if (typeof(link_obj) != "undefined") {
			$(link_obj).addClass("selected");
		} 
		$("#rgift_prod select[name=tshirt_id] option").removeAttr("selected");
		$("#rgift_prod select[name=tshirt_id] option[value="+rg_tshirt_id+"]").attr("selected","selected");
		$("#rgift_prod select[name=tshirt_side] option").removeAttr("selected");
		$("#rgift_prod select[name=tshirt_side] option[value="+rg_tshirt_side+"]").attr("selected","selected");
		if (rg_tshirt_id == 7101) { 
			$("#rgift_prod select[name=tshirt_size] option[value=4]").css("display","none");
			$("#rgift_prod select[name=tshirt_size] option").removeAttr("selected");
		} else { 
			if (typeof($("#rgift_prod select[name=tshirt_size] option[value=4]")) != "undefined") {
				$("#rgift_prod select[name=tshirt_size] option[value=3]").css("display","");
			}
		} 
		return false;
	} else {
		return true;
	}
	
}
function switchCups(pid, c_id, link_obj, c_side) {
	//alert(pid+","+c_id+","+link_obj+","+c_side);
	if (typeof(c_id) != "undefined" && c_id > 0) {
		rg_cup_id = new Number(c_id);
		//rg_tshirt_print = new Number(t_id.substring(1,1));
		//rg_tshirt_print = new String(rg_tshirt_id);
		//rg_tshirt_print = rg_tshirt_print.substring(2,1);
	}
	if (typeof(c_side) != "undefined" && c_side > 0) {
		rg_cup_side = new Number(c_side);
		$("#rgift_prod_images td a").removeClass("selected");
	}
	if (typeof(rg_cup_side) == "undefined") {
		rg_cup_side = 1;
	}
	if (rg_cup_id > 0) {
		$("#bg_print").css("background-image","url('http://ifrype.com/img/gifts/cups/"+pid+"_"+rg_cup_id+"_"+rg_cup_side+".jpg')");
		if (typeof(link_obj) != "undefined") {
			$(link_obj).addClass("selected");
		} 
		$("#rgift_prod select[name=rg_cup_id] option").removeAttr("selected");
		$("#rgift_prod select[name=rg_cup_id] option[value="+rg_cup_id+"]").attr("selected","selected");
		$("#rgift_prod select[name=rg_cup_side] option").removeAttr("selected");
		$("#rgift_prod select[name=rg_cup_side] option[value="+rg_cup_side+"]").attr("selected","selected");
		return false;
	} else {
		return true;
	}
	
}
function img_upl(){
	$('#img_upload_subm').hide();
	$('#loading').show();
	$('#img_upload').submit();
	return false;
};
function addToBasket() {
	var fdata = '';
	var faction = $("form#rgift_prod").attr("action");
	$("form#rgift_prod input").each(
		function () {
			fdata = $(this).attr("name") + ": '" + $(this).val() + "', ";
		}
	);
	//fdata.substr(1,-2);
	alert(fdata);
	$.post(faction,fdata);
	return false;
}
function prod_subm(f) {
	if (typeof $("input[name=product_type]") != "undefined") {
		switch ($("input[name=product_type]").attr("type")) {
			case 'radio':
				if (typeof $("input[name=product_type]:checked") == "undefined" || typeof $("input[name=product_type]:checked").val() == "undefined") {
					txt = $("#prod_type_title").val();
					InfoLite.open({text: '<p>'+txt+'</p>'});
					return false;
				}
			break;
		}
	}
	if (typeof $("#pFrmImgUplCount") != "undefined") {
		var pFrmImgUplCount = $("#pFrmImgUplCount").val();
		if (pFrmImgUplCount > 0) {
			var images = $("input[name=images]").val();
			if (images != "") {
				images = images.split(",");
			} else {
				images = new Array();
			}
			if (count(images) < pFrmImgUplCount) {
				InfoLite.open({text: '<p><b>' + xGifts["add_images"].replace("!count", (pFrmImgUplCount - count(images))) + '!</b></p>'});
				return false;
			}
		}
	}
	return true;
}
startpageRndButtons(0);
showSmsAct(0);
$(document).ready(function(){
    $("#sender_type_select input[type=radio], #sender_type_select label").click(function () {
        sender_type = $("#sender_type_select input:checked").val();
        if (typeof(sender_type) != "undefined") {
		$("#sender_type_input_1, #sender_type_input_2").hide();
		$("#sender_type_input_"+ sender_type).show();
	}
    });
    $("#basket_send_yourself, #basket_send_yourself_label").click(function () {
        if (typeof $("#basket_send_yourself:checked").val() != "undefined") {
        	$("#basket_recipient_info").hide();
        } else {
        	$("#basket_recipient_info").show();
        }
    });
});