
var Feesthoek;
//onload finish make new instance of the class
Event.observe(window,'load',function(){
	Feesthoek = new FeesthoekClass();
});


var FeesthoekClass = Class.create({
	//dom object 
	activeCategoryItem : '',
	activeSubcategoryItem : '',
	buttonLocked:false,
	active_order_show : Array(),
	
	//constructor
	initialize: function(){
		//search for the current active link
		
		
		var active_link = $$('div[class~=categorie_active_link]');
		var active_link = active_link[0];
		if(typeof active_link != 'undefined'){
			this.setActiveLink(active_link);
		}		
		this.startObservers();
	},
	startObservers:function(){
		
		//headertable_
		var headers = $$('td[id^="headertable_"]');
		if(headers.length > 0){
			
			var middles = $$('td[id^="middletable_"]');
			var footers = $$('td[id^="footertable_"]');
			var images = $$('img[id^="middletable_image_"]');
			
			for(i=0;i<headers.length;i++){
				
				Event.observe(headers[i].id, 'mouseover', function(event) {
					try{
					productid = event.target.id;
					productid = productid.replace(/headertable_/,'',productid);
					Feesthoek.productHover(productid);
					}catch(e){}
				});
				Event.observe(headers[i].id, 'mouseout', function(event) {
					try{
					productid = event.target.id;
					productid = productid.replace(/headertable_/,'',productid);
					Feesthoek.productOut(productid);
					}catch(e){}
				});
				
				Event.observe(middles[i].id, 'mouseover', function(event) {
					try{
					productid = event.target.id;
					productid = productid.replace(/middletable_/,'',productid);
					Feesthoek.productHover(productid);
					}catch(e){}
				});
				Event.observe(middles[i].id, 'mouseout', function(event) {
					try{
					productid = event.target.id;
					productid = productid.replace(/middletable_/,'',productid);
					Feesthoek.productOut(productid);
					}catch(e){}
				});
				//middletable_image_
				Event.observe(images[i].id, 'mouseover', function(event) {
					try{
					productid = event.target.id;
					productid = productid.replace(/middletable_image_/,'',productid);
					Feesthoek.productHover(productid);
					}catch(e){}
				});
				Event.observe(images[i].id, 'mouseout', function(event) {
					try{
					productid = event.target.id;
					productid = productid.replace(/middletable_image_/,'',productid);
					Feesthoek.productOut(productid);
					}catch(e){}
				});
			}
		}
		if($('link_account')){
			Event.observe('link_account', 'mouseover', function(event) {
				try{
					$('link_account').addClassName('user_navigation_over');
				}catch(e){}
			});
			Event.observe('link_account', 'mouseout', function(event) {
				try{
					$('link_account').removeClassName('user_navigation_over');
				}catch(e){}
			});
		}
		if($('link_addresses')){
			Event.observe('link_addresses', 'mouseover', function(event) {
				try{
					$('link_addresses').addClassName('user_navigation_over');
				}catch(e){}
			});
			Event.observe('link_addresses', 'mouseout', function(event) {
				try{
					$('link_addresses').removeClassName('user_navigation_over');
				}catch(e){}
			});
		}
	},
	
	//set category items to active
	setActiveLink: function(domobject){
		//remove subcategory active state
		if(this.activeSubcategoryItem != ''){
			this.activeSubcategoryItem.removeClassName('subcategorie_active_link');
		}
		
		//if same item is clicked....
		//pull back
		if(this.activeCategoryItem == domobject){
			//alert('fack');
			domobject.removeClassName('categorie_active_link');
			domobject.next('div').hide();
			
			object = domobject.up('td').previous();
			object.innerHTML = '<img style="display:block;padding-top:3px;" src="'+basepath+'res/images/plus.png" />';
			this.activeCategoryItem = '';
			return;
		}
		//if there is an current active item
		//deactivate that one
		if(this.activeCategoryItem != ''){
			this.activeCategoryItem.removeClassName('categorie_active_link');
			//hide the current subcategory
			this.activeCategoryItem.next('div').hide();
			object = this.activeCategoryItem.up('td').previous();
			object.innerHTML = '<img style="display:block;padding-top:3px;" src="'+basepath+'res/images/plus.png" />';
		}
		//set the new active dom object
		this.activeCategoryItem = domobject;
		
		//set the class for the new dom object
		domobject.addClassName('categorie_active_link');
		
		//---------- subcategory
		//show the current subcategory :)
		domobject.next('div').show();
		
		object = domobject.up('td').previous();
		object.innerHTML = '<img style="display:block;padding-top:3px;" src="'+basepath+'res/images/minus.png" />';
	},
	setActiveSubLink: function(domobject){
		if(this.activeSubcategoryItem != ''){
			this.activeSubcategoryItem.removeClassName('subcategorie_active_link');
		}
		domobject.addClassName('subcategorie_active_link');
		this.activeSubcategoryItem = domobject;
	},
	showCategoryText:function(divId){
		//document.observe("a:show", function(event) {
		 	//alert('a');
			//return;
			//var_dump(event);
		//});
		
		//var someNode = $(divId);
		//someNode.fire("a:show");

		//$(divId).getStyle('display') == 'none';
		//var_dump($(divId));
		$(divId).show();
		
		//Effect.Appear(divId,{duration:0.3});
		//
		
	},
	hideCategoryText:function(divId){
		$(divId).hide();
		//$(divId).fade({ duration: 0.3, from: 1, to: 0 });
		//Effect.DropOut(divId);

	},
	//type 0=productoverview, 1=productview
	addToCart:function(productid,type){
		url = basepath + 'webshop/addtocart';
		//$('product_button_'+productid).disable();
		//alert(basepath);
		//return;
		
		var params = {
			amount: $F(productid+'_amount'),
			productid:productid
		}
		
		var custom = '';
		if($(productid+'_custom_text')){
			custom = $F(productid+'_custom_text');
			params.custom = custom;
		}
		
		new Ajax.Request(url, {
			method: 'post',
			parameters: params,
			onSuccess: function(transport){
				data = transport.responseText.evalJSON(true);
				if(data.error == ''){
					Feesthoek.updateCart(data,type);
				}else{
					alert(data.error);
					//enable button
					//$('product_button_'+productid).enable();
				}
				
			}
		});
	},
	//type 0=productoverview, 1=productview
	updateCart:function(data,type){
		var id = data.id;
		var keystore = data.keystore;
		var amount = data.amount;
		var url = data.url;
		var name = data.name;
		var price = data.price;
		var totalprice = data.totalprice;
		var custom = !Object.isUndefined(data.custom) ? data.custom : '';
		
		if($('totalprice')){
			
			if (totalprice == '0,-') {
				$('shoppingcart_totalprice').innerHTML = '';
			}else{
				$('totalprice').innerHTML = totalprice;
			}
			
		}else{
			var html = '';
			html +='<br/>';
			html +='<hr align="left" noshade="noshade" style="width:140px;color:#FFFFFF;"  />';
			html +='Totaal: &euro; <span id="totalprice">'+totalprice+'</span>';
			$('shoppingcart_totalprice').innerHTML += html;
		}
		
		
		//if item exists... only update the amount
		if($('shoppingcart_product_'+keystore)){
			if(amount == '0'){
				$('shoppingcart_product_'+keystore).remove();
				//$('product_button_'+id).enable();
				//if no products found remove the button!
			}
			var elements = $$('span[id^="shoppingcart_product_"]');
			//alert(elements.length);
			if(elements.length > 0){
				$('basket_submit').show();
			}else{
				$('basket_submit').hide();
			}	
					
			new Effect.Morph('shoppingcart_amount_'+keystore, {
			  style: 'color: #ffffff;', 
			  duration: 2,
			  beforeStart:function(){
			  	if (type == '0') {
					Feesthoek.message(id, 'Het aantal is bijgewerkt.');
				}else{
					//$('product_button_'+id).enable();
					Feesthoek.toCart(id,amount);
				}
			  	$('shoppingcart_amount_'+keystore).setStyle({color:'#80ff00'});
			  	$('shoppingcart_amount_'+keystore).innerHTML = amount;
			  }
			});	
				
		}else{
			var html = '';
			html +='<span id="shoppingcart_product_'+keystore+'">';
			html +=		'<span id="shoppingcart_amount_'+keystore+'">'+amount+'</span>x ';
			html +=		'<a id="shoppingcart_name_'+keystore+'" href="'+url+'">'+name+'</a><br/>';
			if(custom != ''){
				html +=		'<small id="shoppingcart_custom_'+keystore+'" style="margin-left:17px;">'+custom+'</small>';
				html +=		'<br/>';
			}
			
			html +='</span>';
			$('shoppingcart').innerHTML += html;
			$('basket_submit').show();
			
			
			var effectsArray = new Array();
			
			var effect1 = new Effect.Morph('shoppingcart_product_'+keystore, {
			  style: 'color: #ffffff;', 
			  sync:true
			});
			var effect2 = new Effect.Morph('shoppingcart_name_'+keystore, {
			  style: 'color: #ffffff;', 
			  sync:true
			});
			
			
			effectsArray.push(effect1);
			effectsArray.push(effect2);
			new Effect.Parallel(effectsArray,{
				duration:1,
				beforeStart:function(){
					if(type == '0'){
						Feesthoek.message(id,'Product is toegevoegd');
						
					}else{
						Feesthoek.toCart(id,amount);
					}
					$('shoppingcart_product_'+keystore).setStyle({color:'#80ff00'});
					$('shoppingcart_name_'+keystore).setStyle({color:'#80ff00'});
				},
				afterFinish:function(){
					
				}
			});
		}
	},
	//move effect
	//moves and makes small
	toCart:function(id,amount){
		//$('product_button_'+id).enable();
		
		var effectsArray = new Array();
		
		var effect1 = new Effect.Move('product_image_clone', { 
			x: 600, 
			y: 0, 
			sync:true,
			mode: 'relative',
			duration:0.5 
		
		});
		
		var effect2 = new Effect.Morph('product_image_clone', {
		  style: 'width:1px;height:1px;', 
		  sync:true
		});
		
		effectsArray.push(effect1);
		effectsArray.push(effect2);
		
		new Effect.Parallel(effectsArray,{
			duration:1,
			beforeStart:function(){
				//$('product_button_'+id).enable();
			},
			afterFinish:function(){
				//remove the element
				$('product_image_clone').remove();
				//make a brand new element
				var src = $('product_image').src;
				$('product_image_container').innerHTML = '<img id="product_image_clone" src="'+src+'" class="productview_image" style="width:248px;">'
			}
		});
	},
	message:function(id,message){
		//alert(id);
		$('product_message_'+id).innerHTML = message;
		new Effect.Move('product_message_'+id, { 
			x: 0, 
			y: -80, 
			//y: -150, 
			mode: 'relative',
			duration:0.5,
			afterFinish:function(){
				/*new Effect.Fade('product_message_'+id, { 
					duration:0.5,
					afterFinish:function(){
						$('product_message_'+id).setStyle({top:'80px'});
					}
				
				 });*/
				new Effect.Move('product_message_'+id, { 
					x: 0, 
					y: 80, 
					mode: 'relative',
					duration:1,
					delay:1,
					afterFinish:function(){
						//$('product_button_'+id).enable();
					}
				 });
			}
		 });
	},
	productHover:function(id){
		if($('headertable_' + id) && $('middletable_' + id) && $('footertable_' + id)){
			$('headertable_'+id).setStyle({backgroundColor:'#eaeaea'});
			$('middletable_'+id).setStyle({backgroundColor:'#eaeaea'});
			$('footertable_'+id).setStyle({backgroundColor:'#eaeaea'});
		}
		
	},
	productOut:function(id){
		if ($('headertable_' + id) && $('middletable_' + id) && $('footertable_' + id)) {
			$('headertable_' + id).setStyle({
				backgroundColor: '#ffffff'
			});
			$('middletable_' + id).setStyle({
				backgroundColor: '#ffffff'
			});
			$('footertable_' + id).setStyle({
				backgroundColor: '#ffffff'
			});
		}
	},
	redirect:function(url,obj,notvalue){
		if($(obj)){
			if($(obj.value != notvalue)){
				document.location = url;
			}
		}
	},
	order_show:function(id){
		//click
		//order_container_
		var scope = this;
		//order_data_container_
		
		fullId = 'order_data_container_'+id;
		
		//item exists so close it
		if(this.active_order_show[id] == true){
			Effect.SlideUp(fullId,{duration:0.5});
			this.active_order_show[id] = false;
		}else{
			this.active_order_show[id] = true;
			Effect.SlideDown(fullId,{duration:0.5});
		}
		
	},
	showAdditionalAddress:function(){
		if($('additional_address').checked == true){
			$('additional_address_table').show();
		}else{
			$('additional_address_table').hide();
		}
		
		
	},
	showAdditionalAddress2:function(){
		if($('loggedin_additional_address').checked == true){
			$('loggedin_additional_address_table').show();
		}else{
			$('loggedin_additional_address_table').hide();
		}
		
		
	}
	
	
});


function var_dump(element, limit, depth)
{
	depth = depth?depth:0;
	limit = limit?limit:1;
	returnString = '<ol>';
	for(property in element)
	{
		//Property domConfig isn't accessable
		if (property != 'domConfig')
		{
			returnString += '<li><strong>'+ property + '</strong> <small>(' + (typeof element[property]) +')</small>';
			if (typeof element[property] == 'number' || typeof element[property] == 'boolean')
				returnString += ' : <em>' + element[property] + '</em>';
			if (typeof element[property] == 'string' && element[property])
				returnString += ': <div style="background:#C9C9C9;border:1px solid black; overflow:auto;"><code>' +
									element[property].replace(/</g, '<').replace(/>/g, '>') + '</code></div>';
			if ((typeof element[property] == 'object') && (depth <limit))
				returnString += var_dump(element[property], limit, (depth + 1));
			returnString += '</li>';
		}
	}
	returnString += '</ol>';
	if(depth == 0)
	{
		winpop = window.open("", "","width=800,height=600,scrollbars,resizable");
		winpop.document.write('<pre>'+returnString+ '</pre>');
		winpop.document.close();
	}
	return returnString;
}


addEvent(window, 'load', initCorners);
			
  function initCorners() {
    var settings1 = {
      tl: { radius: 20 },
      tr: { radius: 20 },
      antiAlias: true
    }
	
	var settings2 = {
      bl: { radius: 20 },
      br: { radius: 20 },
      antiAlias: true
    }
	var settings = {
      tl: { radius: 20 },
      tr: { radius: 20 },
      bl: { radius: 20},
      br: { radius: 20 },
      antiAlias: true
    }

    var divObj = document.getElementById("myDiv");
	curvyCorners(settings1, "#test");
	curvyCorners(settings2, "#test2");
	 
    curvyCorners(settings, ".round-corners");
}


