// 	jQuery init

//	popup plugin
(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);

//	auth module
function auth_module() {
	$("form[name='auth_form']").submit(function() {
		if($("div.auth input[name='auth_txt_1']").val() != '' && $("div.auth input[name='auth_txt_2']").val() != '') {
			this.submit();
			return true;
		}
		return false;
	});
	
	$("div.auth a.login").click(function() {
		$("form[name='auth_form']").submit();
		return false;
	});
	
	$("div.auth input[name='auth_txt_1']").click(function() {
		if($(this).val() == 'adresa@email.ro') {
			$(this).attr('value', '');
			$("div.auth input[name='auth_txt_2']").attr('value', '');
		}
	});
	
	$("div.auth input[name='auth_txt_2']").click(function() {
		if($(this).val() == '------') {
			$(this).attr('value', '');
			$("div.auth input[name='auth_txt_1']").attr('value', '');
		}
	});
}

//	search module
function search_module() {
	$("div.search_field input[name='src_txt_1']").click(function() {
		if($(this).val() == 'cauta...') {
			$(this).attr('value', '');
		}
	});
	
	$("div.search_field input[name='src_txt_1']").blur(function() {
		if($(this).val() == '') {
			$(this).attr('value', 'cauta...');
		}
	});
	
	$("form[name='search_form']").submit(function() {
		if($("div.search_field input[name='src_txt_1']").val() != 'cauta...' && $("div.search_field input[name='src_txt_1']").val() != '') {
			this.submit();
			return true;
		}
		return false;
	});
	
	$("div.search_field a.search_btn").click(function() {
		$("form[name='search_form']").submit();
		return false;
	});
}

//	filter menu dropdown
function filter_dropdown() {
	$('#filters > ul > li').hover(
		function() { $('ul', this).css('display', 'block'); },
		function() { $('ul', this).css('display', 'none'); }
	);
	
	$('#filters > ul > li > a').click(function() {return false;});
}

//	gallery popup
function gallery_popup() {
	$('.popup').popupWindow({
		height: 750,
		width: 800,
		centerScreen: 1
	});
}

//	close gallery popup
function close_popup() {
	$('.close').click(function() {
		window.close();
	});
}

//	pf_pj switch
function pf_pj_switch(type) {
	if(type == 'pf') {
		$('#pf').removeClass('hidden');
		$('#pj').addClass('hidden');
		$('#reg_invoice_pf').attr('checked', 'checked');
	}
	
	if(type == 'pj') {
		$('#pf').addClass('hidden');
		$('#pj').removeClass('hidden');
		$('#reg_invoice_pj').attr('checked', 'checked');
	}	
}

//	pf_pj change function
function pf_pj_change() {
	$('input[name="reg_invoice_pf_pj"]').click(function() {
		pf_pj_switch($(this).val());
	});
}

//	get cities
function get_cities(cnt_name, selected, target) {
	$("#" + target).load($.base_url + "misc/cities", { 'params[]': [cnt_name, selected, target] });
}

//	populate cities
function populate_cities() {
	$('.populate_cities').change(function() {
		var target	= '';
		if(this.id == 'reg_invoice_pf_txt_3') {
			target	= 'reg_invoice_pf_txt_4';
		}
		if(this.id == 'reg_invoice_pj_txt_4') {
			target	= 'reg_invoice_pj_txt_5';
		}
		get_cities($(this).val(), '', target);
	});
}

//	cart payment method
function payment_method() {
	$("input[name='pm']").click(function() {
		var total_val	= parseFloat($.total_invoice);
		total_val		= parseFloat(total_val.toFixed(2));
		if($(this).val() == 'rmb') {
			$("tr.ramburs").removeClass('hidden');
			$("tr.shipping").removeClass('total_last');
			$("tr.total_invoice span.total_price").text(total_val + 10);
		} else {
			$("tr.ramburs").addClass('hidden');
			$("tr.shipping").addClass('total_last');
			$("tr.total_invoice span.total_price").text(total_val);
		}
	});
}

//	product combinations
function product_cmb() {
	//	enable options dropdowns
	$('select.prod_opt').change(function() {
		var opt_vals	= [];
		var crt = 0;
		$('select.prod_opt').each(function() {
			opt_vals[crt] = $(this).val();
			crt++;
		});
		
		var opt_val1	= 0;
		var opt_val2	= 0;
		
		if(crt == 2) {
			opt_val1	= opt_vals[0];
			opt_val2	= opt_vals[1];
		}
		
		if(crt == 1) {
			opt_val1	= opt_vals[0];
		}
		
		var cmb_vals	= window['cmb_' + $.prod_id];
		var price		= 0;
		var qty			= 0;
		
		for(i = 0; i < cmb_vals.length; i++) {
			if(cmb_vals[i].opt1_id == opt_val1 && cmb_vals[i].opt2_id == opt_val2) {
				price	= cmb_vals[i].price;
				qty		= cmb_vals[i].qty;
			}
		}
		
		//	populate price
		var price_int	= Math.floor(price);
		var price_dec	= parseFloat(price - price_int);
		price_dec		= price_dec.toFixed(2);
		price_dec		= '.' + price_dec.substr(2, 2) + '<br /><span>RON cu TVA</span>';
		
		$('#price_int').text(price_int);
		$('#price_dec').html(price_dec);
		
		//	populate infos
		
		$('#product_detail').text();
		$('#product_stoc').text();
	});
	
	//	enable post buy
	if($.has_opts == true) {
		$('.price_module .buy').click(function() {
			$('form[name="prod_' + $.prod_id + '"]').submit();
			return false;
		});
	}
}

//	...and here we go
$(document).ready(function(){
	auth_module();
	search_module();
	filter_dropdown();
	gallery_popup();
	close_popup();
	pf_pj_change();
	populate_cities();
	payment_method();
	product_cmb();
});
