
window.addEvent('domready',function(){
   	$('artist').rollover('/gfx/artists2.png');
	$('gallery').rollover('/gfx/galleries2.png');
	$('book').rollover('/gfx/books2.png');
	$('corp').rollover('/gfx/corporate2.png');
	$('blog').rollover('/gfx/blog2.png');
		$('cart').rollover('/gfx/cart2.png');
	$('about').rollover('/gfx/about2.png');
	$$('a.target').each(function(elm){elm.set('target','_blank');});
	var menuitem = new EcceMenu();
	
	//Product Page Thumbnails Clicks
	$$('.changethumbnail').each(function(elm){
		elm.addEvent('click',function(){
			var loaddiv = new Element('div', {	'styles': {'height': 0,'overflow': 'hidden'}}).inject($('productimage'),'after');				  
			var pimg = this.getProperty('thumb-img');
			var limg = this.getProperty('thumb-limg');
			
			var loadimg = new Element('img', {'src':pimg,'events': {
				'load': function(){
					$('productimage').setProperty('src',pimg);
					var parent = $('productimage').getParent('a');
					$('productimage').inject(parent,'after');
					parent.dispose();
					MojoZoom.makeZoomable($('productimage'),limg);	
					loaddiv.dispose();
				}
			}
			}).inject(loaddiv);

		});
	});
	//Category Text Expand
	
	
	if($chk($('opentext')) && $chk($('categorytext'))){
		$('opentext').addEvent('click',function(e){
			e.stop();
			var textsize = $('categorytext').getScrollSize();
			var myFx = new Fx.Tween($('categorytext'));
			myFx.start('height', textsize.y);
			this.setStyle('display','none');
		});
	}
	if($chk($('opentext')) && $chk($('closetext')) && $chk($('categorytext'))){
		$('closetext').addEvent('click',function(e){
			e.stop();
			var myFx = new Fx.Tween($('categorytext'),{'onComplete':function(){$('opentext').setStyle('display','block');}});
			myFx.start('height', 0);
			
		});
	}
	//Author Text Expand
	if($chk($('authoropentext')) && $chk($('authortext'))){
		$('authoropentext').addEvent('click',function(e){
			e.stop();
			var textsize = $('authortext').getScrollSize();
			var myFx = new Fx.Tween($('authortext'));
			myFx.start('height', textsize.y);
		});
	}
	if($chk($('authoropentext')) && $chk($('authorclosetext')) && $chk($('authortext'))){
		$('authorclosetext').addEvent('click',function(e){
			e.stop();
			var myFx = new Fx.Tween($('authortext'));
			myFx.start('height', 0);
		});
	}
	
	
	if($chk($('loadlink'))){
		$('loadlink').addEvent('change',function(){
			window.location = this.value;
			this.disabled=true;
		});
	}
	
	// Approved Stockist
	if($chk($('stockistmap'))){
		var CurrentImage = $('stockistmap').getProperty('src');
		var maptext=new Array();
		maptext[0]="southeast";
		maptext[1]="london";
		maptext[2]="eastanglia";
		maptext[3]="southwest";	
		maptext[4]="midlands";
		maptext[5]="north";
		maptext[6]="scotland";
		
		for(i=0;i<maptext.length;i++){
			if($chk($('map'+maptext[i])) && $chk($(maptext[i]))){
				preload= new Image(50,50); 
				preload.src='/gfx/map'+maptext[i]+'.png'; 
				$('map'+maptext[i]).addEvent('mouseenter',function(){
					 $('stockistmap').setProperty('src','/gfx/'+this.getProperty('id')+'.png');
				});
				$('map'+maptext[i]).addEvent('mouseout',function(){
					 $('stockistmap').setProperty('src',CurrentImage);
				});
				$('map'+maptext[i]).addEvent('click',function(e){
					e.stop();
					CurrentImage = '/gfx/'+this.getProperty('id')+'.png';
					$$('.approvedstockists').each(function(elm){
						if(elm.getSize().y>0){
							var myFx = new Fx.Tween(elm);
							myFx.start('height', 0);
						}
					});
					text = this.id.substr(3,this.id.length);
					var myFx2 = new Fx.Tween($(text));
					myFx2.start('height', $(text).getScrollSize().y);
				});
			}
		}
	}
	
	//Framing Popup
	if($chk($('framing')) && $chk($('popframing'))){
		$('popframing').addEvent('click',function(e){
			e.stop();
			if($('framing').value==0){
				alert("You need to choose a frame first");
			}else{
				Slimbox.open('/framing/image/'+this.getProperty('productid')+'/'+$('framing').value,'Please Note: print and frame not to scale');
			}
		});
	}
	
});




Element.implement({
	verticlealign: function(to) {
		var newsize = ((to.getSize().y-this.getSize().y)/2).toInt()-25;
		if(newsize<0) newsize =0;
		this.setStyle("margin-top",newsize);
		return this;
	 },
	rollover: function(img) {
		preload= new Image(50,50); 
		preload.src=img; 
		var timag = this.getProperty("src");
		this.addEvent('mouseenter',function(){this.setProperty("src",img);});
		this.addEvent('mouseleave',function(){this.setProperty("src",timag);});
		return this;
	}     		   
});

var EcceMenu = new Class({
	Implements: [Options],				
	options: {
		'selection': '.menuitem',
		'menuitem': 'div',
		'duration':300,
		'closedelay':300
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var c = this;
		op.id = new Array();
		
		$$(this.options.selection).each(function(elm,i){
				elm.setStyles({'position':'absolute','overflow':'hidden','height':0});
				var parent = elm.getParent();
				parent.addEvent('mouseenter',function(){
					var size = elm.getScrollSize();
					var myFx = new Fx.Tween(elm,{'duration':op.duration});
					myFx.start('height', size.y);
				});
				parent.addEvent('mouseover',function(){
					$clear(op.id[i]);
				});
				parent.addEvent('mouseleave',function(){
					$clear(op.id[i]);
					op.id[i] = c.closeelm.delay(op.closedelay,c,elm);
				});
				elm.addEvent('mouseleave',function(){
					op.id[i] = c.closeelm.delay(op.closedelay,c,elm);
				});
				elm.addEvent('mouseenter',function(){
					$clear(op.id[i]);
				});
		});
		
	},
	closeelm: function(elm){
		var op = this.options;
		var myFx2 = new Fx.Tween(elm,{'duration':op.duration});
		myFx2.start('height', 0);	
	}
});

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var MultipleSelect = new Class({
	Implements: [Options],
	options: {
		'unwantedoptions': 'unwanted',
		'wantedoptions': 'admin_access',
		'submitbutton': 'submitbutton',
		'addbutton': 'add',
		'removebutton': 'remove'
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var allchecked = true;
		if($chk($(op.unwantedoptions))==false) allchecked = false;
		if($chk($(op.wantedoptions))==false) allchecked = false;
		if($chk($(op.submitbutton))==false) allchecked = false;
		if($chk($(op.addbutton))==false) allchecked = false;
		if($chk($(op.removebutton))==false) allchecked = false;
		if(allchecked){
			$(op.addbutton).addEvent('click',function(e){
				e.stop();
				$(op.unwantedoptions).getChildren('option').each(function(elm){
						if(elm.selected) elm.inject($(op.wantedoptions));
				});
			});
			$(op.removebutton).addEvent('click',function(e){
				e.stop();
				$(op.wantedoptions).getChildren('option').each(function(elm){
						if(elm.selected) elm.inject($(op.unwantedoptions));
				});
			});
			$(op.submitbutton).addEvent('click',function(e){
				var values='';
				$(op.wantedoptions).getChildren('option').each(function(elm){
						values= values+elm.value+',';
				});		
				$(op.hidden).value = values;
			});
		}else{
			alert("Multiple Select Can't be Initialized");
		}
	}
});
