// JavaScript Document

var PopupEditor = Class.create();

PopupEditor.prototype = {
	
    initialize: function() {    
        
        this.updateList();

        var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div',{id:'overlay'}));
	
        objBody.appendChild(Builder.node('div',{id:'popupEditor'},
		[
			Builder.node('div',{id:'popupEditor-container'},
			[
				Builder.node('div',{id:'popupEditor-close',align:'right'},
				[
					Builder.node('div',{id:'close'},
					[
						Builder.node('img',{src:'images/greenbox.gif',width:'23',height:'22',border:'0'})
					]),
				]),
				Builder.node('div',{id:'price-container'},
				[
					Builder.node('div',{id:'price-darkwood'},				// DARK WOOD PRICE
					[
						Builder.node('img',{src:'images/personalized price/darkwood.jpg',width:'550',height:'61'}),
						Builder.node('div',{className:'small-spacer'},''),
						Builder.node('div',{className:'wordsprice'},'PRICE'),
						Builder.node('div',{className:'pricingbordercontainer'},
						[
							Builder.node('img',{src:'images/personalized price/darkwoodpricing.jpg',width:'361',height:'404'})
						])
					]),
					Builder.node('div',{id:'price-lightwood'},				// LIGHT WOOD PRICE
					[
						Builder.node('img',{src:'images/personalized price/lightwood.jpg',width:'550',height:'68'}),
						Builder.node('div',{className:'small-spacer'},''),
						Builder.node('div',{className:'wordsprice'},'PRICE'),
						Builder.node('div',{className:'pricingbordercontainer'},
						[
							Builder.node('img',{src:'images/personalized price/lightwoodpricing.jpg',width:'363',height:'404'})
						])
					]),
					Builder.node('div',{id:'price-brownwood'},				// BROWN WOOD PRICE
					[
						Builder.node('img',{src:'images/personalized price/brownwood.jpg',width:'550',height:'68'}),
						Builder.node('div',{className:'small-spacer'},''),
						Builder.node('div',{className:'wordsprice'},'PRICE'),
						Builder.node('div',{className:'pricingbordercontainer'},
						[
							Builder.node('img',{src:'images/personalized price/brownwoodpricing.jpg',width:'363',height:'406'})
						])
					]),
					Builder.node('div',{id:'features-lightwood'},
					[
						Builder.node('img',{src:'images/personalized price/lightwoodfeaturetitle.jpg',width:'650',height:'72'}),
						Builder.node('div',{className:'small-spacer'},''),
						Builder.node('div',{className:'wordsprice'},'PRODUCT FEATURES'),
						Builder.node('img',{src:'images/personalized price/lightwoodfeatures.jpg',width:'650',height:'372'})
					]),
					Builder.node('div',{id:'features-darkwood'},
					[
						Builder.node('img',{src:'images/personalized price/darkwoodfeaturetitle.jpg',width:'650',height:'69'}),
						Builder.node('div',{className:'small-spacer'},''),
						Builder.node('div',{className:'wordsprice'},'PRODUCT FEATURES'),
						Builder.node('img',{src:'images/personalized price/darkwoodfeatures.jpg',width:'650',height:'369'})
					]),
					Builder.node('div',{id:'features-brownwood'},
					[
						Builder.node('img',{src:'images/personalized price/brownwoodfeaturetitle.jpg',width:'650',height:'69'}),
						Builder.node('div',{className:'small-spacer'},''),
						Builder.node('div',{className:'wordsprice'},'PRODUCT FEATURES'),
						Builder.node('img',{src:'images/personalized price/brownwoodfeatures.jpg',width:'650',height:'369'})
					]),
					Builder.node('div',{id:'images-light-1'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5178_crop.jpg',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-light-2'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5205.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-light-3'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5239.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-light-4'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5203_H2.jpg',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-light-5'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5195.JPG',width:'600',height:'400'}),
					]),
					
					Builder.node('div',{id:'images-brown-1'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5233_mod.jpg',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-brown-2'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5015_mod2.jpg',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-brown-3'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5225.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-brown-4'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5220.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-brown-5'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5249.JPG',width:'600',height:'400'}),
					]),
					
					Builder.node('div',{id:'images-dark-1'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5033.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-dark-2'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5052.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-dark-3'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5075_crop.jpg',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-dark-4'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5084.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'images-dark-5'},
					[
						Builder.node('img',{src:'images/Chopsticks/IMG_5041.JPG',width:'600',height:'400'}),
					]),
					Builder.node('div',{id:'price-giftwrap'},				// GiftWrap
					[
						Builder.node('div',{id:'price-giftwrap-box'},
						[
						 	Builder.node('img',{id:'green-x-img',src:'images/greenbox.png',width:'23',height:'22',border:'0'})
						]),
					])
				])
			])
		]
		));


		$('overlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('popupEditor').hide().observe('click', (function(event) { if (event.element().id == 'price') this.end(); }).bind(this));
		$('close').observe('click', (function() { this.end(); }).bind(this));
		$('price-container').hide();
		$('price-darkwood').hide();
		$('price-lightwood').hide();
		$('price-brownwood').hide();
		$('features-darkwood').hide();
		$('features-lightwood').hide();
		$('features-brownwood').hide();
		$('price-giftwrap').hide().observe('click', (function() { this.end(); }).bind(this));
		$('images-light-1').hide();
		$('images-light-2').hide();
		$('images-light-3').hide();
		$('images-light-4').hide();
		$('images-light-5').hide();
		$('images-brown-1').hide();
		$('images-brown-2').hide();
		$('images-brown-3').hide();
		$('images-brown-4').hide();
		$('images-brown-5').hide();
		$('images-dark-1').hide();
		$('images-dark-2').hide();
		$('images-dark-3').hide();
		$('images-dark-4').hide();
		$('images-dark-5').hide();


		var th = this;
        (function(){
            var ids = 
                'overlay popupEditor popupEditor-container popupEditor-close price-container';   
            $w(ids).each(function(id){ th[id] = $(id); });
        }).defer();
    },
	
    updateList: function() {   
        this.updateList = Prototype.emptyFunction;

        document.observe('click', (function(event){
            var target = event.findElement('a[rel^=popup]') || event.findElement('area[rel^=popup]');
            if (target) {
                event.stop();
                this.start(target);
            }
        }).bind(this));
    },

	
    start: function(info) {
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
        var objBody = $$('body')[0];
		var halfWidth = 290;
		$('popupEditor').setStyle({width:'552px'});
		$('price-container').setStyle({width:'550px'});
		$('price-container').show();
		
		if(info.href.indexOf('http://suk.shockforcestudio.com/') == 0)
		{
			var actionString = info.href.gsub('http://suk.shockforcestudio.com/',''); // HACK //
			var action = actionString.split("=");
		}
		else if(info.href.indexOf('http://localhost/Suk/') == 0)
		{
			var actionString = info.href.gsub('http://localhost/Suk/',''); // HACK //
			var action = actionString.split("=");
		}
		else if(info.href.indexOf('http://chopchopsticks.com/') == 0)
		{
			var actionString = info.href.gsub('http://chopchopsticks.com/',''); // HACK //
			var action = actionString.split("=");
		}
		else if(info.href.indexOf('https://chopchopsticks.com/') == 0)
		{
			var actionString = info.href.gsub('https://chopchopsticks.com/',''); // HACK //
			var action = actionString.split("=");
		}
		else if(info.href.indexOf('http://www.chopchopsticks.com/') == 0)
		{
			var actionString = info.href.gsub('http://www.chopchopsticks.com/',''); // HACK //
			var action = actionString.split("=");
		}
		else if(info.href.indexOf('https://www.chopchopsticks.com/') == 0)
		{
			var actionString = info.href.gsub('https://www.chopchopsticks.com/',''); // HACK //
			var action = actionString.split("=");
		}
		switch(action[0])
		{
			case 'price' :
			
				switch(action[1])
				{
					case 'brownwood' : $('price-brownwood').show();break;
					case 'lightwood' : $('price-lightwood').show();break;
					case 'darkwood'  : $('price-darkwood').show();break;
					case 'giftwrap'  :
						$('popupEditor').setStyle({width:'394px',height:'340px'});
						$('price-container').setStyle({width:'392px'});
						halfWidth = 190;
						$('price-giftwrap').show();
						$('popupEditor-close').hide();break;
				}
				break;
				
			case 'features' :
			
				$('popupEditor').setStyle({width:'654px',height:'349px'});
				$('price-container').setStyle({width:'652px'});
				halfWidth = 340;
				switch(action[1])
				{
					case 'brownwood' : $('features-brownwood').show();break;
					case 'lightwood' : $('features-lightwood').show();break;
					case 'darkwood'  : $('features-darkwood').show();break;
				}
				break;
			case 'images'	:
				$('popupEditor').setStyle({width:'600px',height:'402px'});
				$('price-container').setStyle({width:'600px'});
				switch(action[1])
				{
					case 'light1' : $('images-light-1').show(); break;
					case 'light2' : $('images-light-2').show(); break;
					case 'light3' : $('images-light-3').show(); break;
					case 'light4' : $('images-light-4').show(); break;
					case 'light5' : $('images-light-5').show(); break;
					case 'brown1' : $('images-brown-1').show(); break;
					case 'brown2' : $('images-brown-2').show(); break;
					case 'brown3' : $('images-brown-3').show(); break;
					case 'brown4' : $('images-brown-4').show(); break;
					case 'brown5' : $('images-brown-5').show(); break;
					case 'dark1' : $('images-dark-1').show(); break;
					case 'dark2' : $('images-dark-2').show(); break;
					case 'dark3' : $('images-dark-3').show(); break;
					case 'dark4' : $('images-dark-4').show(); break;
					case 'dark5' : $('images-dark-5').show(); break;
				}
				break;
		}
		// stretch overlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
        new Effect.Appear(this.overlay, { duration: 0.2, from: 0.0, to: 0.5 });

        // calculate top and left offset for the lightbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var lightboxLeft = arrayPageScroll[0];
        this.popupEditor.setStyle({ top: 100 + 'px', left: lightboxLeft+(arrayPageSize[0]/2)-halfWidth + 'px' });
		new Effect.BlindDown(this.popupEditor, { duration: 0.5 });
    },
	
    end: function() {
		$('popupEditor').value = "";
 		$('price-container').hide();
		$('price-darkwood').hide();
		$('price-lightwood').hide();
		$('price-brownwood').hide();
		$('features-darkwood').hide();
		$('features-lightwood').hide();
		$('features-brownwood').hide();
		$('price-giftwrap').hide();
		$('images-light-1').hide();
		$('images-light-2').hide();
		$('images-light-3').hide();
		$('images-light-4').hide();
		$('images-light-5').hide();
		$('images-brown-1').hide();
		$('images-brown-2').hide();
		$('images-brown-3').hide();
		$('images-brown-4').hide();
		$('images-brown-5').hide();
		$('images-dark-1').hide();
		$('images-dark-2').hide();
		$('images-dark-3').hide();
		$('images-dark-4').hide();
		$('images-dark-5').hide();
      this.popupEditor.hide();
        new Effect.Fade(this.overlay, { duration: 0.2 });
		var objBody = $$('body')[0];
		objBody.setStyle({ overflow:'auto' });
       $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
    },
	
    getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
};
document.observe('dom:loaded', function () { new PopupEditor(); });
