
Site = {
	
	init: function () {
		Site.windowLoadedInit();
		Site.setupNav();
		Site.setupFlash();
		Site.init_preview_slider();
		Site.init_signoff_slider();
		Site.init_save_design();
	},
	
	init_save_design: function(){

		if ($('saved')){
			
			  $$('ul#saved input.bt').each(function(el){
			  el.addEvent('click', function(ev){
					if (confirm('Are you sure you want to delete this?')){
						this.getParent().action.value = this.getAttribute('rel');
						return true;
					}
					return false;
				});
			  });			
			
		}
	}, 
	
	init_signoff_slider: function(){

		if ($('productDetails2')){
			if (!$('productDetails2').getElement('li.adv')){
				return;	
			}
			var width = 468;
			$('unpersonalize').addEvent('click', function(event){
				event.stop();
				$('productDetails2').getElement('ul.paginator').get('tween').start('left', 0 * width, -1 * width);
				$('quantity_in_extra').value = $('quantity').value;
			});	
		}
	}, 
	
	init_preview_slider: function () {
		if ($('previewer')){
			var width = 500;
			var cont = $('dpreview');
			var triggers = $$('#previewer div#darrows img');
			
			if (!cont || !triggers) return;

			// Default tab
			var selectedTab = $('dpreview').getElement('li.selected');
			var current_page = (selectedTab ? $('dpreview').getElements('li').indexOf(selectedTab) : 0);
			cont.set('tween', { 'link': 'cancel' });
			cont.get('tween').set('left', -(current_page * width));
			
			triggers.each(function (trigger, i) {
				trigger.addEvent('click', function (event) {
					if (i != current_page) {
						cont.get('tween').start('left', -(current_page * width), -(i * width));

						$('darrows').getElement('img.selected').change();//removeClass('selected');
						$('darrows').getElements('img')[i].addClass('selected');
						
						$('dpreview').getElement('li.selected').removeClass('selected');
						$('dpreview').getElements('li')[i].addClass('selected');
						
						current_page = i;
					}
				});
			});
			
			$$('#darrows img').each(function (img) { 
				img.addEvent('mouseenter', function(){
					if (!this.hasClass('selected')){
						img.src=img.src.replace("_off.gif", ".gif");	
					}
				});
				img.addEvent('mouseleave', function(){
					if (!this.hasClass('selected')){
						img.src=img.src.replace(".gif", "_off.gif");	
					}
				});
				img.change = function(){
					if (this.hasClass('selected')){
						img.src=img.src.replace(".gif", "_off.gif");	
					}
					this.removeClass('selected');	
				}
				if (img.hasClass('selected')){
						img.src=img.src.replace("_off.gif", ".gif");	
				}
			});	
		}
	},	
	setupNav: function () {
		$$('div#header div#navigation > ul > li').addEvents({
			'mouseenter': function() {
				this.addClass('current');
			},
			'mouseleave': function() {
				this.removeClass('current');
			}
		});		
	},
	
	setupFlash: function () {
		if ($('promoHolder')) {
			new Swiff('../flash/promo.swf', {
				width:  321, 
				height: 163,
				container: $('promoHolder')
			});
		}
	},
	
	/*
	 * init for any methods / functions that need
	 * to be run only on window load.
	 */
	windowLoadedInit: function () {
		$$('ul#listing > li img.mainImage').each(function (img) { 
			new ImagePopup(img);
		});
		$$('ul#saved > li input').each(function (img) { 
			img.addEvent('mouseenter', function(){
				img.src=img.src.replace(".gif", "_over.gif");	
			});
			img.addEvent('mouseleave', function(){
				img.src=img.src.replace("_over.gif", ".gif");	
			});
		});
		$$('ul#saved input.send_email').each(function(el){
			el.onclick = function(evt){
				openmypage(this.getParent().designid.value);
				return false;	
			}
		});
			
	}
	
};


var ImagePopup = new Class({
	
	Implements: Options,
	
	img: null,
	li: null,
	container: null,
	top: null,
	left: null,
	
	options: {
		width: 270,
		height: 270,
		padding: 10,
		fadeDuration: 0
	},
	
	initialize: function (img, options) {
		this.img  = img;
		this.li   = img.getParent('li');
		
		this.a = this.li.getElement('a');
		
		
		
		this.setOptions(options);
		
		this.top  = -((this.options.height + this.options.padding * 2 - this.img.getSize().y) / 2);
		this.left = -((this.options.width + this.options.padding * 2 - this.li.getSize().x) / 2);
		
		this.container = new Element('div', {
			'class': 'imgPopup',
			'width': this.options.width,
			'height': this.options.width,
			'styles': {
				'padding': 10,
				'border': '1px solid #cccccc',
				'background-color': '#ffffff',
				'position': 'absolute',
				'opacity': 0,
				'z-index': 600,
				'top': this.top,
				'left': this.left
			}
		});
		
		var src = img.src.replace(/\d+x\d+/i, this.options.width + "x" + this.options.height);
		
		new Element('img', { 'src': src, 'width': 270, 'height': 270 }).inject(this.container);
		new Element('a', { 'href': this.a.get('href') }).inject(this.container);
		
		this.container.getElement('img').inject(this.container.getElement('a'),'top');
		

		
		this.img.addEvent('mouseenter', this.show.bindWithEvent(this));
		this.container.addEvent('mouseleave', this.hide.bindWithEvent(this));
	},
	
	show: function () {
		// temporary! make sure no other popups are around before loading the current one.
		$$('.imgPopup').fireEvent('mouseleave');
		
		this.container.inject(this.li, 'top');
		this.container.get('tween', { property: 'opacity', duration: this.options.fadeDuration }).start(1);
	},
	
	hide: function () {
		this.container.get('tween', {
			property: 'opacity',
			duration: this.options.fadeDuration,
			onComplete: function () {
				this.container = this.container.dispose();
			}.bindWithEvent(this)
		}).start(0);
	}
	
});


function add_special_req() {
	
	var myRequest = new Request({method: 'post', url: '/special_rqmnts.php'});
	
	
	myRequest.onSuccess = function(res) {
		$('sreq').set('style','display : block');
		$('sreq').innerHTML = res;
	}
	
	
	
	myRequest.send('objid='+$('obj_key').value + '&text='+$('spl_text').value );
}

window.addEvent('domready', Site.init);
