var Site = new Class({
	ldr: null,
	ldr_info: null,
	ldr_img: null,
	active_menu: null,
	smenu: null,
	initialize: function()
	{
		if ($('loader') != null) this.ldr = $('loader');
		if ($('loader-info') != null) this.ldr_info = $('loader-info');
		if ($('loader-img') != null) this.ldr_img = $('loader-img');
		this.create_shops_links();
		this.init_social_links();
		//if (_banner_status != 0) var slide = new Fx.Slide('pusher').hide();
	},
	show_loader: function()
	{
		site.ldr.style.display = 'block';
		site.ldr_img.style.display = 'inline';
		var s = site.ldr.getSize();
		site.ldr.style.left = ((window.getWidth() / 2) - (s.x / 2)) + 'px';
		site.ldr.style.top = ((((window.getHeight() / 2)) - (s.y / 2)) + window.getScrollTop()) + 'px';
	},
	show_cart_loader: function(sn)
	{
		site.ldr_img.style.display = 'none';
		site.ldr_info.innerHTML = 'Item has been added to your cart!<br />\
		<br /><a href=""onclick="site.hide_loader();return(false);">Continue Shopping</a>&nbsp;&nbsp;<a href="/cart/checkout/' + sn + '/">Checkout</a>';
		site.ldr.style.display = 'block';
		var s = site.ldr.getSize();
		site.ldr.style.left = ((window.getWidth() / 2) - (s.x / 2)) + 'px';
		site.ldr.style.top = ((((window.getHeight() / 2)) - (s.y / 2)) + window.getScrollTop()) + 'px';
	},
	loader_show_error: function(msg)
	{
		if (msg == null)
		{
			//msg = ro_error.substr(1);
			alert('No message to display');
			return;
		}
		site.ldr_img.style.display = 'none';
		site.ldr_info.innerHTML = msg + '<br /><br /><a href=""onclick="site.hide_loader();return(false);">Close this</a>';
		site.ldr.style.display = 'block';
		var s = site.ldr.getSize();
		site.ldr.style.left = ((window.getWidth() / 2) - (s.x / 2)) + 'px';
		site.ldr.style.top = ((((window.getHeight() / 2)) - (s.y / 2)) + window.getScrollTop()) + 'px';
	},
	hide_loader: function()
	{
		site.ldr.style.display = 'none';
		site.ldr_info.innerHTML = 'Please wait, processing your request...';
	},
	init_tips: function()
	{
		var Tips1 = new Tips($$('.ttips'), {'showDelay': 100, 'hideDelay': 100});
		$$('.ttips').removeProperties('alt', 'title');
	},
	warn_logout: function()
	{
		if (confirm('If you cart is not empty, you will lose all pending orders once you log out.\nContinue with logout?'))
		{
			window.location = '/accounts/logout/';
		}
			
	},
	do_banner_switch: function(status)
	{
		if (_banner_status == 0)
		{
			var slide = new Fx.Slide('pusher', {'duration': 1000, onComplete: function(){
				_banner_status = 1;
				var json = new Request.JSON({url: '/async/banner-switch/', onSuccess: function(rj, rt){
				}}).send('status=1');
			}}).slideOut();
		}
		else
		{
			var slide = new Fx.Slide('pusher', {'duration': 1000, onComplete: function(){
				_banner_status = 0;
				var json = new Request.JSON({url: '/async/banner-switch/', onSuccess: function(rj, rt){
				}}).send('status=0');
			}}).slideIn();
		}
	},
	init_imgpreview: function()
	{
		var box = $('imgprevbox');
		$each($$('a.imgprev'), function(a, i){
			var pos = a.getPosition();
			var el = new Element('div', {'class': 'imgprevbox', 'id': 'el_' + i});
			var timg = a.getFirst();
			timg.removeProperty('alt');
			var img = new Element('img', {'src': timg.src.replace(/\/t/, '/p')});
			el.fade('hide');
			$$('body').adopt(img); // to get image size
			var s = img.getSize();
			if (s.y == 0) s.y = 2;
			el.grab(img);
			el.setStyle('top', (pos.y - 60) - (s.y / 2));
			el.setStyle('left', 120 + pos.x);
			box.grab(el);
			a.addEvent('mouseover', function(){el.fade('in');});
			a.addEvent('mouseout', function(){el.fade('out');});
		});
	},
	create_shops_links: function()
	{
		if (shops.length <= 0) return;
		this.smenu = new Element('ul', {'class': 'shops-links', 'id': 'shops_list'});
		var li = null;
		var a = $('shops_links');
		var vis = null;
		for (var i = 0; i < shops.length; i++)
		{
			var li =  new Element('li');
			var aa = new Element('a', {'html': shops[i][0], 'href': '/shops/' + shops[i][1] + '/'})
			aa.addEvent('mouseover', function(){vis = true;});
			if (i == shops.length - 1) li.setStyle('border', 'none');
			this.smenu.grab(li.grab(aa));
		}
		this.smenu.inject($('content'));
		a.addEvent('mouseover', function(){
			vis = true;
			this.smenu.position({'relativeTo': a, 'position': 'bottomLeft', 'edge': 'upperLeft', 'offset': {'x': 0, 'y': 8}});
			setTimeout(function(){if (!vis) this.smenu.setStyle('display', 'none');}.bind(this), 1000);
			this.smenu.setStyle('display', 'block');
		}.bind(this));
		a.addEvent('mouseout', function(){
			vis = false;
		}.bind(this));
		this.smenu.addEvent('mouseover', function(){
			vis = true;
		}.bind(this));
		this.smenu.addEvent('mouseout', function(){
			vis = false;
			//this.smenu.setStyle('display', 'none');
			setTimeout(function(){if (!vis) this.smenu.setStyle('display', 'none');}.bind(this), 1000);
		}.bind(this));
	},
	change_sort_order: function(el, url)
	{
		if (el.value == 0) return;
		if (url == '') return;
		var goto = '';
		if (url.match(/\?c=/g))
			goto = url + '&s=' + el.value;
		else
			goto = url + '?s=' + el.value;
		window.location = goto;
	},
	init_social_links: function()
	{
		var t = $('_twitter');
		var f = $('_facebook');
		var y = $('_youtube');
		t.addEvent('mouseover', function(){t.childNodes[0].src = '/images/social_twitter_off.jpg';});
		t.addEvent('mouseleave', function(){t.childNodes[0].src = '/images/social_twitter_on.jpg';});
		f.addEvent('mouseover', function(){f.childNodes[0].src = '/images/social_facebook_off.jpg';});
		f.addEvent('mouseleave', function(){f.childNodes[0].src = '/images/social_facebook_on.jpg';});
		y.addEvent('mouseover', function(){y.childNodes[0].src = '/images/social_youtube_off.jpg';});
		y.addEvent('mouseleave', function(){y.childNodes[0].src = '/images/social_youtube_on.jpg';});
	},
	popup: function(url){window.open(url, '', 'menubar=no,width=760,height=570,toolbar=no');},
	popup2: function(url){window.open(url, '', 'menubar=no,width=760,height=700,toolbar=no');},
	set_img: function (obj, img, status){if (site.active_menu != img) obj.childNodes[0].src = '/images/site/euro-coins/commemorative-coins/nav_' + img + '_' + status + '.jpg';}
});
var Cart = new Class({
	initialize: function()
	{
	},
	add_to_cart: function(iid,sid, sn)
	{
		site.show_loader();
		var quant = $('quant');
		var q = quant.value;
		var len = quant.options.length;
		if (q == 0)
		{
			site.hide_loader();
			return;
		}
		var json = new Request.JSON({url: '/cart/add-item/', onSuccess: function(rj, rt){
			site.hide_loader();
			if ($('cart_total') == null) cart.create_cart_module(sn);
			$('cart_total').set('html', rj.cost);
			$('cart_quant').set('html', rj.quant);
			for (i = quant.options.length - 1; i >= 0; i--) quant.remove(i);
			for (i = 1; i <= (len - rj.item_quant); i++) quant.options.add(new Option(i, i));
			if ($('ce') != null) $('ce').dispose();
			if ($('ac') == null) $('cart').grab(new Element('a', {'id': 'ac', 'href': '/cart/checkout/' + sn + '/', 'class': 'cl'}).set('html', 'Checkout'), 'bottom');
			site.show_cart_loader(sn);
		}}).send('iid=' + iid + '&sid=' + sid + '&quant=' + q);
	},
	add_to_cart_quick: function(iid,sid, sn)
	{
		site.show_loader();
		var q = 1;
		var json = new Request.JSON({url: '/cart/add-item/', onSuccess: function(rj, rt){
			site.hide_loader();
			if ($('cart_total') == null) cart.create_cart_module(sn);
			$('cart_total').set('html', rj.cost);
			$('cart_quant').set('html', rj.quant);
			if ($('ce') != null) $('ce').dispose();
			if ($('ac') == null) $('cart').grab(new Element('a', {'id': 'ac', 'href': '/cart/checkout/' + sn + '/', 'class': 'cl'}).set('html', 'Checkout'), 'bottom');
		}}).send('iid=' + iid + '&sid=' + sid + '&quant=' + q);
	},
	create_cart_module: function(sn)
	{
		//var cart = $('cart');
		/*if (cart == null)
		{
			cart = new Element('div', {id: 'cart'});
			$('sidebar').inject(cart);
		}*/
		$('cart').set('html', '<span class="sec sc">Your Cart</span><div class="cart"><p><span id="cart_quant"></span> item(s) in your cart</p><p>Total: <span id="cart_total"></span> &euro;</p><a href="/cart/checkout/' + sn + '/">Checkout</a></div>');
	},
	do_submit: function(sn, id, logged)
	{
		var scost = $('shipping_cost').value;
		if (scost == 0)
		{
			site.loader_show_error('You must select a shipping method before submitting your order');
			return;
		}
		var fname = $('fname').value;
		var lname = $('lname').value;
		var address = $('address').value;
		var pcode = $('pcode').value;
		var city = $('city').value;
		var state = $('state').value;
		var country = $('country').value;
		var phone = $('phone').value;
		var email = $('email').value;
		var cemail = $('cemail').value;
		var popt = $('popt').value;
		var msg = $('msg').value;
		if (fname == '' || lname == '' || address == '' || pcode == '' || city == '' || state == '' || country == 0 || phone == '' || email == '' || cemail == '' || popt == 0)
		{
			site.loader_show_error('All fields are mandatory, please fill all fields and submit your order again');
			return;
		}
		if (logged == 0) if (!confirm('For a better shopping experience your data will be stored safely at Coins World. This will ease your future purchases by not having to type your information again, you will just have to log in to the site. \n\nIf you don\'t wish for your details to be stored, please abort checkout.')) return;
		var vars = 'sid=' + id + '&fname=' + escape(fname).replace(/\\+/g,'%2B') + '&lname=' + escape(lname).replace(/\\+/g,'%2B') + '&address=' + escape(address).replace(/\\+/g,'%2B') 
		+ '&pcode=' + escape(pcode).replace(/\\+/g,'%2B') + '&city=' + escape(city).replace(/\\+/g,'%2B') + '&state=' + escape(state).replace(/\\+/g,'%2B') 
		+ '&country=' + parseInt(country) + '&phone=' + escape(phone).replace(/\\+/g,'%2B') + '&email=' + escape(email).replace(/\\+/g,'%2B') + '&cemail=' + escape(cemail).replace(/\\+/g,'%2B') + '&popt=' + parseInt(popt) + '&msg=' + escape(msg).replace(/\\+/g,'%2B') + '&shipid=' + parseInt(scost);
		site.show_loader();
		var html = new Request.HTML({url: '/cart/store/', onSuccess: function(rt, rel, rhtml, rjs){
				if (rhtml == '')
					window.location = '/cart/confirm/' + sn + '/';
				else
					site.loader_show_error(rhtml);
			}}).send(vars);
	},
	remove_item: function(sid, iid)
	{
		if (!confirm('Remove this item from your order?')) return;
		if (sid != 0 || iid != 0)
		{
			site.show_loader();
			var html = new Request.HTML({url: '/cart/remove-item/', onSuccess: function(rt, rel, rhtml, rjs){
					window.location.reload();
			}}).send('sid=' + sid + '&iid=' + iid);
		}
	},
	make_shipping_cost: function(obj)
	{
		if (obj.value <= 0)
		{
			$('scost').set('html',  'n/a');
			$('total_order').set('html',  'n/a');
			$('shipping_cost').value = 0;
			return;
		}
		if (smeth.length <= 0)
		{
			alert('Error retrieving shipping costs. Your order will be processed \nwith 0 (zero) shipping cost and merchant will calculate it \nmanually before payment.');
			return;
		}
		for (var i = 0; i < smeth.length; i++)
		{
			if (smeth[i][0] == obj.value)
			{
				var q = parseInt($('quantity').value);
				var cost = smeth[i][1];
				if (q > 1)
				{
					q = q - 1;
					cost += (q * smeth[i][2]);
				}
				$('scost').set('html',  cost + ' EUR');
				$('total_order').set('html',  (($('subtotal').value * 1) + cost));
				$('shipping_cost').value = obj.value;
				break;
			}
		}
	}
});
var site = null;
var cart = null;
window.addEvent('domready', function(){site = new Site();cart = new Cart();if(typeof(doimgs) != 'undefined') site.init_imgpreview();if(typeof(dotips) != 'undefined') site.init_tips();});