
function show_pop(fil){
	if ($('box_overlay')){
		$('box_overlay').remove();
		$('box_wrapper').remove();
	}
	
	var overlay = Builder.node('div', { id: 'box_overlay',style: 'display:none' });
	var element = Builder.node('div', { id: 'box_wrapper'}, [
	  	
		Builder.node('div', { id: 'box_rep' }),
		
	  ]);
	  
	$('body').appendChild(overlay);
	$('body').appendChild(element);
	$('box_overlay').observe('click', (function() { $('box_overlay').remove();$('box_wrapper').remove() }));
	$('box_overlay').appear({ duration: 0.3, from: 0.0, to: 0.8 });
	
	new Ajax.Updater({ success: 'box_rep' }, fil, {});
}

/* Nytt popup script 31.08 */
var pop = {
 	init: function(file,type,width,title){
 		pop.destroy();
		pop.build_window(type,width,title);
		pop.update_content(file, type);
	},
	build_window: function(type,width,title){
		var overlay = Builder.node('div', { id: 'box_overlay', style: 'display:none' });
		
		/* Legg til nye type her hvis du vil bygge ut. */
		switch(type){
			case 1:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_login' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			case 2:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_glemt' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
		    case 3:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_video' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			case 4:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_tilbakemelding' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			 case 5:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_vilkar' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			case 6:
				var element = Builder.node('div', { id: 'box_wrapper', style: 'display:none;width:' + width + 'px;margin-left:-' + parseInt(width/2) + 'px;'}, [
				  	Builder.node('div', { id: 'box_top' }, title),
					Builder.node('div', { id: 'box_rep', className: 'box_rep_passordsendt' }),
					Builder.node('div', { id: 'box_close' })
				  ]);
				  break;
			break;
		}
		  
		document.body.appendChild(overlay);
		document.body.appendChild(element);
		
		$('box_overlay').observe('click', (function() { pop.destroy(); }));
		$('box_close').observe('click', (function() { pop.destroy(); }));
		$('box_overlay').appear({ duration: 0.3, from: 0.0, to: 0.5, queue: 'end' });
		$('box_wrapper').appear({ duration: 0.3, from: 0.0, to: 1.0, queue: 'end' });
	},
	update_content: function(file){
		new Ajax.Updater({ success: 'box_rep' }, file, {});
	},
	destroy: function(){
 		if ($('box_overlay')){
			$('box_overlay').remove();
			$('box_wrapper').remove();
		}
	}
};
/* ===== */

function hover(elementid,image)
	{
		$(elementid).setAttribute("src", image);
	}
	
function hoverout(elementid,image,m,m2)
	{
		//alert(m + " " + m2);
		//alert($(elementid).getAttribute("src"));
		if(m != m2){
			$(elementid).setAttribute("src", image);
		}else{
			$(elementid).setAttribute("src",$(elementid).getAttribute("src"));
		}
	}

function galleri(){
	var i = 1;
	var u = 20;
	var periodical = true;
	var arr = $('flash').childElements();
	var ubound = arr.size()-1
	$('prev').hide();
	$('next').hide();
	
	$('flash').observe("mouseover",function(){
		
		$('prev').show();
		$('next').show();
	});
	
	/*
	$('flash').observe("mouseout",function(){
		periodical = true;
		$('prev').hide();
		$('next').hide();
	});
	*/
	
	new PeriodicalExecuter(function(){
		if(periodical){
			(i >= ubound) ? i = 0 : i += 1;
			slide();
		}
	},5);
	
	$('prev').observe("click",function(){
		periodical = false;
		(i <= 0) ? i = ubound : i -= 1;
		slide();
	});
	
	$('next').observe("click",function(){
		periodical = false;
		(i >= ubound) ? i = 0 : i += 1;
		slide();
	});
	
	function slide(){
		u += 1
		arr[i].hide();
		arr[i].setStyle({ zIndex: (u) });
		Effect.Appear(arr[i], { duration: 0.5 });
	}
}

function hover_IMG(arg)
	{
		var elem = arg;
		
		
		alert(elem);
	}


function swapImg(arg)
	{
		var elem = $('prod_img');
		var elem2 = $('zoom');
		elem.src = 'http://kundeweb.aggressive.no/users/danielfranck.no/webshop/medium/'+arg;
		elem2.setAttribute('href','http://kundeweb.aggressive.no/users/danielfranck.no/webshop/large/'+arg);
		
	}

function showit(arg)
	{
		var elem = $(arg);
		elem.style.visibility = 'visible';
	}
	
function hideit(arg)
	{
		var elem = $(arg);
		elem.style.visibility = 'hidden';
	}

function add()
	{
		if(!$("addToBag")) return false;
		var addToBag = $("addToBag");
				
		addToBag.onsubmit = function()
			{
				var errormsg = null;
				var errormsg = "Please complete the following fields:" + "\n";
				var valid = true;
				
				var size = $("storrelse").getValue();
				var colour = $("farge").getValue();
				
				if(colour == "colour")
					{
						valid = false;
						errormsg += "- Please pic your colour" + "\n";
					}
				if(size == "size")
					{
						valid = false;
						errormsg += "- Please pic your size" + "\n";
					}
					
				if(valid)
					{
						return true;
					}else{
						alert(errormsg);
						return false;
					}
				
			}
		 
	}
	
function bestilling()
	{
		if(!$("bestilling")) return false;
		var bestilling = $("bestilling");
				
		bestilling.onsubmit = function()
			{
				var errormsg = null;
				var errormsg = "Fyll ut følgende felter:" + "\n";
				var valid = true;
				var color_ok = '#00ff00';
				var color_not_ok = '#ff0000';
								
				fornavn = $("fornavn").getValue();
				etternavn = $("etternavn").getValue();
				epost = $("epost").getValue();
				adresse = $("adresse").getValue();
				postnr = $("postnr").getValue();
				poststed = $("poststed").getValue();
								
				if(fornavn.length < 2)
					{
						valid = false
						$('fornavn').setStyle({background: color_not_ok});
						errormsg = errormsg + "- Firstname" + "\n";
					}else{
						$('fornavn').setStyle({background: color_ok});
					}
				if(etternavn.length < 2)
					{
						valid = false
						$('etternavn').setStyle({background: color_not_ok});
						errormsg = errormsg + "- Lastname" + "\n";
					}else{
						$('etternavn').setStyle({background: color_ok});
					}
				if(adresse.length < 2)
					{
						valid = false
						$('adresse').setStyle({background: color_not_ok});
						errormsg = errormsg + "- Address" + "\n";
					}else{
						$('adresse').setStyle({background: color_ok});
					}
				if(postnr.length < 4 || isNaN(postnr))
					{
						valid = false;
						$('postnr').setStyle({background: color_not_ok});
						errormsg = errormsg + "- Zipcode" + "\n";
					}else{
						$('postnr').setStyle({background:  color_ok});
					}
				if(poststed.length < 2)
					{
						valid = false;
						$('poststed').setStyle({background: color_not_ok});
						errormsg = errormsg + "- City" + "\n";
					}else{
						$('poststed').setStyle({background: color_ok});
					}
				
				if(epost.length < 4 || epost.indexOf("@") == -1 || epost.indexOf(".") == -1)
					{
						valid = false;
						$('epost').setStyle({background: color_not_ok});
						errormsg = errormsg + "- E-mail" + "\n";
					}else{
						$('epost').setStyle({background: color_ok});
					}
					
				
				
												
				if(valid)
					{
						return true;
					}else{
						alert(errormsg);
						return false;
					}
				
			}
	}
	
var f = {
	init: function(){
		
		$('bestilling').select('input').each(function(s){
			s.observe('blur', f.sjekk_element);
		});
	},
	sjekk_element: function(e) {
		var obj = Event.element(e);
		var rel = obj.getAttribute('rel');
		var color_ok = '#00ff00';
		var color_not_ok = '#ff0000';
		var valid = true;
				
		if(rel == "text")
			{
				if(obj.value.length < 3)
					{
						obj.setStyle({background: color_not_ok});
						valid = false;
					}else{
						obj.setStyle({background: color_ok});
					}
			}
			
		if(rel == "epost")
			{
				if(obj.value.indexOf("@") == -1 || obj.value.indexOf(".") == -1)
					{
						obj.setStyle({background: color_not_ok});
						valid = false;
					}else{
						obj.setStyle({background: color_ok});
					}
			}
			
		if(rel == "nummer")
			{
				if(isNaN(obj.value) || obj.value.length < 4)
					{
						obj.setStyle({background: color_not_ok});
						valid = false;
					}else{
						obj.setStyle({background: color_ok});
					}
			}
				
	}
}

document.observe("dom:loaded",function(){
	add();
	if($('bestilling'))	{f.init();bestilling();};
	if($('flash')) {galleri();}
})

