

window.addEvent('load', function() {
	//var popViews = new PopView();
	
	var galleryOptionsLinkBtn = new Element('a', { 'id': 'galleryOptionsLink', 'href':'#galleryOptions', 'rel':'modal'	}).inject( $('gallery') ,'before').set('text','Gallery Options');
		
	var optionsModal = new EleModal($('galleryOptionsLink'));
	
	
	
	$('galleryOptions').getElements('a.all').each(function(item,index){
		item.addEvents({
			'click':function(e){
				e = new Event(e).stop();
				checkAll(item);				
			}
		});
	});
	
	$('galleryOptions').getElements('a.none').each(function(item,index){
		item.addEvents({
			'click':function(e){
				e = new Event(e).stop();
				unCheckAll(item);				
			}
		});
	});
	
	
	$('galleryOptionsSaveBtn').addEvents({
		'click':function(e){
			e = new Event(e).stop();
			saveOptions();			
		}
	});
	
	
});

function saveOptions(){
	var preview = "";
	var passedVars = getUrlVars();
	if(passedVars.preview && passedVars.preview == 'true'){	preview = "&preview=true";	}
	
	// Get Stamps Used Option
	var stampsUsed="";
	$(document.body).getElements('#galleryOptions div.left_3column input').each(function(item,index){
		if(item.get('checked')){
			stampsUsed += item.get('value')+"-";
		}
	});	
	if(stampsUsed.contains("-")){ 
		stampsUsed = "stampsUsed="+stampsUsed.substring(0,stampsUsed.length-1); 
	}else{
		stampsUsed = "stampsUsed=none"; 
	}
	
	// Get Team Members Option
	var teamMembers="";
	$(document.body).getElements('#galleryOptions div.middle_3column input').each(function(item,index){
		if(item.get('checked')){
			teamMembers += item.get('value')+"-";
		}
	});	
	if(teamMembers.contains("-")){ 
		teamMembers = "teamMembers="+teamMembers.substring(0,teamMembers.length-1); 
	}else{
		teamMembers = "teamMembers=none";
	}

	// Get Categories Option
	var categories ="";
	$(document.body).getElements('#galleryOptions div.right_3column input').each(function(item,index){
		if(item.get('checked')){
			categories += item.get('value')+"-";
		}
	});	
	if(categories.contains("-")){ 
		categories = "categories="+categories.substring(0,categories.length-1); 
	}else{
		categories = "categories=none";
	}

	// Get Sort Order Option
	var sortOrder = 'sortOrder='+$('sortOrder').get('value');	
	
	//Get Images Per Page Option
	var imagesPerPage = 'imagesPerPage='+$('imagesPerPage').get('value');	
	
	var qString = "?page=1&"+stampsUsed +"&"+ teamMembers +"&"+ categories +"&"+ sortOrder +"&"+ imagesPerPage + preview;
	var destination = ((window.location).toString().split("?"))[0]+qString;
	window.location = destination;
	
}

function getUrlVars()
{
    // Read a page's GET URL variables and return them as an associative array.
    //this function is from http://snipplr.com/view/799/get-url-variables/
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}


function checkAll(item){
	$(item.getParent()).getElements('input').each(function(chkBox,index){
		chkBox.set('checked',true);
	});
}

function unCheckAll(item){
	$(item.getParent()).getElements('input').each(function(chkBox,index){
		chkBox.set('checked',false);
	});
}

var PopView = new Class({
	options: {
    	selector:'.album img',
		imgWidth:325,
		imgHeight:325,
		cropRatio:'1:1'
    },
	
	thumbs:null,
	popDiv:null,
	
	initialize: function(options){
        this.setOptions(options);
		this.thumbs = $(document.body).getElements(this.options.selector);
		this.popDiv = new Element('div', { 'class': 'popDiv'	}).inject( $(document.body) ,'inside').setStyles({'width':this.options.imgWidth,'height':this.options.imgHeight});
		this.arrow = new Element('div', { 'class': 'popDivArrow'	}).inject( this.popDiv ,'inside');
		this.image = new Asset.image().inject(this.popDiv,'inside');
		
		this.thumbs.each(function(item,index){
			
			//change this later!!
			var newsrc = item.src;
			
			var large = new Asset.image(newsrc).inject(this.popDiv,'inside').setStyle('display','none').set('width',this.options.imgWidth).set('height',this.options.imgHeight);
			$(item).store('largeImg', large);
			
			item.addEvents({
				
				'mousemove': function(e){
					e = new Event(e);
					this.over(e,item);					
				}.bind(this),
				
				'mouseenter': function(e){
					e = new Event(e);
					this.over(e,item);
					
					
				}.bind(this),
				
				'mouseleave':function(e){
					e = new Event(e);
					this.out(e);
				}.bind(this)
			});
		}.bind(this));

		this.popDiv.addEvents({
			'mouseenter':function(e){
				e = new Event(e);
				this.out(e);
			}.bind(this)
		});
		
		


    },
	
	out:function(e){
		this.popDiv.setStyles({'display':'none','left':0,'top':0});	
	},
	over:function(e,item){
		e.stop();
		
		var newsrc = item.src.replace('140',this.options.imgWidth).replace('140',this.options.imgHeight);
		
		//this.image.src = newsrc;
		$(document.body).getElements('.popDiv img').each(function(item,index){
			item.setStyle('display','none');
		});
		
		item.retrieve('largeImg').setStyle('display','block');
		
		
		var top = e.client.y - this.popDiv.getCoordinates().height + $(document.body).getScroll().y - 45-15;//minus 45 for arrow
		this.popDiv.setStyles({'display':'block','left':e.client.x,'top':top});	
	},
	
	

	getOrSetId: function(obj){
		if(!obj.id){
			var d = new Date();
			var milli = d.getMilliseconds().toString();
			var randomNumber = Math.floor(Math.random()*1000);
			if(obj.nodeName){
				obj.id = obj.nodeName.toString() + '-' + milli + randomNumber.toString();
			}else{
				obj.id = "element" + milli + randomNumber.toString();	
			}
		}
		return obj.id;
	} 
});
PopView.implement(new Options);
PopView.implement(new Events);


