
function initialize()
{
	iBox.initialize();
	iBox.addEventListener( 'hide', product_image_zoom.__onZoomClose );

	/*
	if( window.history && 1>=window.history.length ) {
		var buttons = $$('.productNavigation .back');
		for( var i=0; i<buttons.length; i++ ) {
			var button = buttons[i];
			button.style.display = 'none';
		}
	}
	*/
	
	$('container').scrollTo();

	styled_select.init();
	
	image_pager.init();
	
	var details = $('productDetails');
	if( details.className=='Set' )
		set_image_zoom.init();
	else
		product_image_zoom.init();

	color_recommendations.init();
	size_selector.init( '#productActions ');
	add_to_cart.init();
	
	rating.init();
	product_sets_pager.init();
	
	send_as_ecard.init();
	ask_a_friend.init();
	print_link.init();
}
Element.observe( window, 'load', initialize, false );

var color_recommendations = {
	init: function() {
		var colors = $$('#productDescription .colors .color');
		for( var i=0; i<colors.length; i++ ) {
			color = colors[i];
			color.observe('click', color_recommendations.__onClicked.bindAsEventListener(color) );
		}
	}
	
	,__onClicked: function( ev ) {
		var ajaxUrl = this.select('.ajaxUrl');
		if( 1!=ajaxUrl.length ) return;
		ajaxUrl = DOM_getText( ajaxUrl[0] );
		
		var ajaxData = {
			method: 'get', 
			parameters: '', 
			onSuccess: color_recommendations.__onAjaxSuccess.bind(this), 
			onFailure: color_recommendations.__onAjaxFailure.bind(this)
		};
		new Ajax.Request( ajaxUrl, ajaxData );
		
		Event.stop (ev);
		return false;
	}
	
	,__onAjaxSuccess: function( req ) {
		//update product
		var details = $('product');
		if( null!=details ){
			styled_select.dispose( '#productActions ');
		
			var oldDiv = details;
			var newDiv = document.createElement(oldDiv.tagName);
			
			newDiv.id = oldDiv.id;
			newDiv.className = oldDiv.className;
			newDiv.innerHTML = req.responseText;
			
			oldDiv.parentNode.replaceChild(newDiv, oldDiv);
			//Element.replace( minicart.firstChild, responseText );
			//minicart.innerHTML = responseText;

			//reinitialize
			styled_select.init( '#productActions ');
			image_pager.init();
			product_image_zoom.init();
		
			color_recommendations.init();
			size_selector.init( '#productActions ');
			add_to_cart.init();
			
			product_sets_pager.init();
			
			ask_a_friend.init();
			print_link.init();
		}
	}
	
	,__onAjaxFailure: function( req ) {
	}
};

var size_selector = {
	current_size: {}
	,selector_map: {}
	,styled_selector_map: {}

	,init: function( base ) {
		var masterid = $$(base+'.sizeselection .masterid');
		masterid = !empty(masterid) ? masterid[0].value : null;
		size_selector.current_size[base] = masterid;
		
		var entries = $$(base+'.sizelist .option');
		for( var i=0; i<entries.length; i++ ){
    		var entry = entries[i];
     		entry.observe('click', size_selector.__onButtonClicked.bindAsEventListener(entry, base) );
		}

		// styled select
		var selectors = $$(base+'.sizeselection .styledselect');
		for( var i=0; i<selectors.length; i++ ){
			var selector = selectors[i];
			styled_select.addHandler( selector.id, size_selector.__onStyledSelectorChanged.bind(selector, base) ); 
     		size_selector.styled_selector_map[base] = {}
     		var options = selector.select('.options div');
     		for(var j=0; j<options.length; j++ ) {
     			var option = options[j];
     			var value = DOM_getText( option.select('.value')[0] );
     			size_selector.styled_selector_map[base][value] = option.select('.label')[0].innerHTML;
     		}
		}
		// normal select
		var selectors = $$(base+'.sizeselection select');
		for( var i=0; i<selectors.length; i++ ){
			var selector = selectors[i];
     		selector.observe('change', size_selector.__onSelectorChanged.bindAsEventListener(selector, base) );
     		size_selector.selector_map[base] = {}
     		for(var i=0; i<selector.options.length; i++ )
     			size_selector.selector_map[base][ selector.options[i].value ] = i;
		}
		
		var id = $$(base+'.sizeselection .selectedid');
		if( !empty(id) ) id = DOM_getText( id[0] );
		if( !empty(id) ) selectSize( id, base );
	}
	
	,selectSize: function( id, base ){
		if( id==size_selector.current_size[base] ) return;
		this.current_size[base] = id;
		
		//update hidden field
		var field = $$(base + '.pid');
		for( var i=0; i<field.length; i++ )
			field[i].value = id;
		
		//update styled selectBox
		var selector = $$(base + '.sizeselection .styledselect .line .title');
		if( !empty(selector) ) {
			var label = size_selector.styled_selector_map[base][id];
			for( var i=0; i<selector.length; i++ ) {
				selector[i].innerHTML = label;
			}
		}
				
		//update select
		var selector = $$(base + '.sizeselection select');
		for( var i=0; i<selector.length; i++ )
			selector[i].selectedIndex = size_selector.selector_map[base][id];
		
		//update price
		var productPricing = $$(base + '.visible .pricing');
		var selectedPricing = ''!=id ? $$(base + '.option' + id + ' .pricing') : [];
		if( !empty(productPricing) && !empty(selectedPricing) )
			productPricing[0].innerHTML = selectedPricing[0].innerHTML;
		
		//update availability
		var selectedAvailable = ''!=id ? $$(base + '.option' + id + ' .available') : [];
		var productAvailable = $$(base + '.product_available');
		for( var i=0; i<productAvailable.length; i++ ) {
			productAvailable[i].style.display = ( empty(selectedAvailable) ) ? 'none' : 'block';
		}
		var selectedSoldout = ''!=id ? $$(base + '.option' + id + ' .soldout') : [];
		var productSoldout = $$(base + '.product_soldout');
		for( var i=0; i<productSoldout.length; i++ ){
			productSoldout[i].style.display = ( empty(selectedSoldout) ) ? 'none' : 'block';
		}
	}
	
	,__onButtonClicked: function(ev, base) {
		var id = this.select('.id');
		if( !empty(id) )
			size_selector.selectSize( id[0].innerHTML, base );	
	}
	
	,__onSelectorChanged: function(ev, base) {
		size_selector.selectSize( this.options[ this.selectedIndex ].value, base );
	}
	
	,__onStyledSelectorChanged: function(base, option) {
		var id = option.select('.value');
		if( !empty(id) )
			size_selector.selectSize( id[0].innerHTML, base );
	}
};

var image_pager = {
	current_image: 'image0'
	,current_offset: 0
	,image_count: null
	,display_count: 3

	,init: function() {
		var total = $$('#productDescription .otherImages .number_pager .total');
		if( 0==total.length ) return;
		image_pager.image_count = parseInt( total[0].innerHTML );
		
		var next_page = $$('#productDescription .otherImages .button_pager .next');
		var prev_page = $$('#productDescription .otherImages .button_pager .previous');
		if( 0!=next_page.length && 0!=prev_page.length ){
			next_page[0].observe('click', image_pager.__onNextClicked.bindAsEventListener(next_page[0]) );
			prev_page[0].observe('click', image_pager.__onPrevClicked.bindAsEventListener(prev_page[0]) );
		}
		
		var images = $$('#productDescription .otherImages .list a');
		for( var i=0; i < images.length; i++ ) {
			var image = images[i];
			image.observe('click', image_pager.__onImageClicked.bindAsEventListener(image) );
			if( image.className=='background_active' && image.id!=image_pager.current_image ) {
				image_pager.setActiveImage( image.id );
			}
		}
		
		
	}
	
	,__onNextClicked: function(ev){
		if( image_pager.current_offset+image_pager.display_count < image_pager.image_count ){
			var hideImage = $('image'+(image_pager.current_offset));
			var showImage = $('image'+(image_pager.current_offset+image_pager.display_count) );
			image_pager.current_offset += 1;
			hideImage.style.display = 'none';
			showImage.style.display = 'block';
		}
	}
	
	,__onPrevClicked: function(ev){
		if( image_pager.current_offset > 0 ){
			image_pager.current_offset -= 1;
			var hideImage = $('image'+(image_pager.current_offset+image_pager.display_count));
			var showImage = $('image'+(image_pager.current_offset));
			hideImage.style.display = 'none';
			showImage.style.display = 'block';
		}
	}
	
	,setActiveImage: function( image_id ) {
		if( image_id==image_pager.current_image ) return;
		var imageUrl = $$('#'+image_id+' .large_image');
		var image = $(image_id);
		var productImage = $('product_image');
		var decoration = $$('#productImage .decoration, #productImage .decorationText');
		var oldImage = $(image_pager.current_image);
		var pagerCurrent = $$('#productDescription .otherImages .number_pager .current');
		var imageIndex = parseInt(image_id.charAt(5));
		
		if( !empty(imageUrl) && null!=productImage ){
			//update selected image in imagelist
			if(null!=oldImage) oldImage.className = 'background';
			image_pager.current_image = image_id;
			image.className = 'background_active';
			//update large Image
			imageUrl = DOM_getText( imageUrl[0] );
			//fix decoration
			for( var i=0; i < decoration.length; i++ )
				decoration[i].style.display = image_id=='image0' ? '' : 'none';
			//update number pager
			if( !empty(pagerCurrent) )
				pagerCurrent[0].innerHTML = imageIndex + 1;
				
			newImage = new Image();	
			newImage.onload = function( ev ) { productImage.src = imageUrl; };
			newImage.src = imageUrl;
		}
	}
	
	,__onImageClicked: function(ev){
		image_pager.setActiveImage( this.id );
	}
	
	,waitForImage: function(img){
		if('undefined'==img || img.onerror){
			//alert( 'error: ' );
			return;
		}

		if(!img.complete){
			//alert( 'ccomp: ' + ' -' + img.complete );
			var waitfor = window.setTimeout("image_pager.waitForImage(img)", 250);
		}

	}	
	
};


function loadNewsImage( newPic ) {
	newPic.src;
}	

product_image_zoom = {
    active: false
    
	,init: function() {
		var zooms = $$('#productImage .zoom, #productImage .rotate');
		for( var i=0; i < zooms.length; i++ ) {
			zooms[i].observe('click', product_image_zoom.__onZoomClicked.bindAsEventListener(zooms[i]) );
		}
	}
	
	,__onZoomClicked: function( ev ) {
		var imageZoom = $('imageZoom');
		var zoomProductName = DOM_getText( $('zoomProductName') );
		var zoomImagePath = DOM_getText( $('zoomImagePath') );
		var zoomFlashPath = DOM_getText( $('zoomFlashPath') );
		var rotates = $$('#productImage .rotate');
		if( empty(imageZoom) || empty(zoomProductName) || empty(zoomImagePath) || empty(zoomFlashPath) ) return;
		var imageIndex = parseInt(image_pager.current_image.charAt(5));
		if( this.className=="rotate" ) imageIndex = 5; 

		var wrapper = $('ibox_wrapper');
		wrapper.style.background = "transparent";
		wrapper.style.border = "none";
		wrapper.style.padding = "0";
	
		iBox.show( imageZoom.innerHTML, null, { width: '819', height: '575', can_resize: false } );
		
		var swf = new SWFObject( zoomFlashPath, "flash_content", "819", "570", "0", "#ffffff");

		swf.addParam("quality", "high"); // Parameter anhängen
		swf.addParam("scale", "noscale");
		swf.addParam("allowFullScreen", "false");
	    //swf.addParam("wmode", "opaque" ); 
		swf.addParam("allowScriptAccess", "always");
		
		swf.addVariable("productName", zoomProductName );
		swf.addVariable("imgPath", zoomImagePath );
		swf.addVariable("act3D", (rotates.length==0 ? "0" : "1") );
		swf.addVariable("timestamp", (new Date()).getTime());
		swf.addVariable("numFirstAkt", imageIndex );
		//swf.addVariable("actZoom", (this.className=="zoom"? '1' : '0') );
		swf.addVariable("functionClose",'iBox.hide');
		swf.write("ibox_content");
		
		var content = $('ibox_content');
		content.style.padding = "0";
		content.style.background = "transparent";
		content.style.overflow = "visible";
		var footer = $('ibox_footer_wrapper');
		footer.style.display = "none";
		
		product_image_zoom.active = true; 
		ev.stop();
		Event.stop(ev); 
		return false;
		
	}
	,__onZoomClose: function( ev ) {
		if( !product_image_zoom.active ) return;
		product_image_zoom.active = false;
		var ibox = $('ibox');
		ibox.remove();
		iBox.initialize();
	}
};

var product_sets_pager = {

	init: function() {
		var showButton = $('productSetShowButton');
		if( null!=showButton )
			showButton.observe('click', product_sets_pager.__onShowClicked.bindAsEventListener(showButton), false );
		
		var previousButton = $('previousSetButton');
		var previousButtonUrl = $('previousSetButtonUrl');
		
		if( null!=previousButton && null!=previousButtonUrl )
			previousButton.observe('click', product_sets_pager.__onPagerClicked.bindAsEventListener(previousButton), false );

		var nextButton = $('nextSetButton');
		var nextButtonUrl = $('nextSetButtonUrl');
		if( null!=nextButton && null!=nextButtonUrl )
			nextButton.observe('click', product_sets_pager.__onPagerClicked.bindAsEventListener(nextButton), false );
	}
	
	,__onShowClicked: function( ev ) {
		var image = $('productSetImage');
		if( null!=image ) {
			document.location.href = image.href;
		 	ev.stop();
		 	return false;
		}
	}
	
	,__onPagerClicked: function( ev ) {
		//perform Ajax Request
		var ajaxUrl = $(this.id+'Url');
		if( null!=ajaxUrl ){
			ajaxUrl = ajaxUrl.href;
			var ajaxData = {
				method: 'get', 
				parameters: '', 
				onSuccess: product_sets_pager.__onAjaxSuccess.bind(this), 
				onFailure: product_sets_pager.__onAjaxFailure.bind(this)
			};
			new Ajax.Request( ajaxUrl, ajaxData );
			
			Event.stop (ev);
			return false;
		}
	}
	
	,__onAjaxSuccess: function( req ) {
		//update minicart
		var productSets = $('productSets');
		if( null!=productSets ){
			var oldDiv = productSets;
			var newDiv = document.createElement(oldDiv.tagName);
			
			newDiv.id = oldDiv.id;
			newDiv.className = oldDiv.className;
			newDiv.innerHTML = req.responseText;
			
			oldDiv.parentNode.replaceChild(newDiv, oldDiv);
			//Element.replace( minicart.firstChild, responseText );
			//minicart.innerHTML = responseText;
		}
		
		product_sets_pager.init();
	}
	
	,__onAjaxFailure: function( req ) {
	}
};

var add_to_cart = {
	
	
	init: function( base ){
		var button = $('addtoCartButton');
		if( null!=button )
			Element.extend(button.form).observe('submit', add_to_cart.__onClicked.bindAsEventListener(button, '#productActions '), false );
			
		var care_products = $$('#productCare .product-micro');
		for( var i=0; i<care_products.length; i++ ){
			var product = care_products[i];
			var form = product.select( 'form' );
			var button = product.select( '.addtocart' );
			if( !empty(form) && !empty(button) )
				button[0].observe('click', add_to_cart.__onClicked.bindAsEventListener(button[0], '#productCare .product-micro #'+product.id+' ', form[0] ), false );
		}
	}
	
	,__onClicked: function(ev, base, form) {
	
		if( empty(form) ) form = Element.extend(this.form);

		//check that cookies are enabled
		if( navigator.cookieEnabled==0 || !document.cookie.match(/dw/) ) 
   		{
			iBox.show( $('cookie_error').innerHTML );

			Event.stop(ev);
			return false;
		}
		
		/* arB: do no longer perform this, instead show the ajax dialog to select the size!
		//check that size was selected
		var size = $('sizeselection');
		if( null!=size ){ //only check if selector is there otherwise no size-variation was available
			var size = size.options[ size.selectedIndex ].value;
			if( ''==size ){
				iBox.show( $('sizeselection_error').innerHTML );
				
				Event.stop(ev);
				return false;
			}
		}
		*/
		
		//check that product is available
		var available = $$(base + '.availability .product_available');
		if( !empty(available) && available[0].style.display!='block' && form.pid.value != form.masterid.value ){
			iBox.show( $('available_error').innerHTML );
			
			Event.stop(ev);
			return false;
		}

		//disable the form
		var postBody = Form.serialize(form); //after disable this wont work!
		Form.disable( form );
		
		//activate loading animation
		var pane = $('addtocart_pane');
		if( null!=pane )
			pane.className = 'addtocart_loading';
		
		iBox.hide();
		
		//perform Ajax Request
		var ajaxUrl = DOM_getText( $('addtominicart_url') );
		if( !empty(ajaxUrl) ){
			var ajaxData = {
				method: 'post', 
				postBody: postBody, 
				onSuccess: add_to_cart.__onAjaxSuccess.bind(this, form), 
				onFailure: add_to_cart.__onAjaxFailure.bind(this, form)
			};
			new Ajax.Request( ajaxUrl, ajaxData );
			
			Event.stop (ev);
			return false;
		}
		else
			add_to_cart.__onAjaxFailure.call( this, form ); 
	}
	
	,__finishLoading: function( form ){
		Form.enable( form ); //disable the form
		
		//deactivate loading animation
		var pane = $('addtocart_pane');
		if( null!=pane ) pane.className = 'addtocart';
	}
	
	,__onAjaxSuccess: function(form, req ){
		add_to_cart.__finishLoading( form );

		//update minicart
		var minicart = $('minicart');
		if( null!=minicart ){
			var oldDiv = minicart;
			var newDiv = document.createElement(oldDiv.tagName);
			
			newDiv.id = oldDiv.id;
			newDiv.className = oldDiv.className;
			newDiv.innerHTML = req.responseText;
			
			oldDiv.parentNode.replaceChild(newDiv, oldDiv);
			//Element.replace( minicart.firstChild, responseText );
			//minicart.innerHTML = responseText;
		}
			
		
		//open lightbox layer
		var minicartContent = $('minicart_content');
		if( null!=minicartContent ){
			
			iBox.show( minicartContent );
		}
			
		var button = $('cartAddToBasketButton');
		if( null!=button ){
			Element.extend(button.form).observe('submit', add_to_cart.__onClicked.bindAsEventListener( button, '#minicart_content ' ), false );
			
			styled_select.init( '#minicart_content ', 'ibox_styledselectOptions' );
			size_selector.init( '#minicart_content ');
		}
	}
	
	,__onAjaxFailure: function( form, req ){
		add_to_cart.__finishLoading( form );
	}
};


rating = {
 oldRating: 0
 ,isHover: false
 ,isActive: false
 ,ajaxUrl: null

 ,init: function() { 
 	var ratingValue = $$('.rating .stars .value');
 	if( !empty(ratingValue) )
 	  rating.oldRating = parseInt( ratingValue[0].innerHTML ) * 0.5;
 	
 	var ajaxUrl = $$('.rating .rateUrl');
 	if( !empty(ajaxUrl) ) 
 	  rating.ajaxUrl = DOM_getText( ajaxUrl[0] );

	if( !empty(rating.ajaxUrl) ){
	 	var stars = $$('.rating .stars .star');
	 	for( var i=0; i<stars.length; i++ ){
	 	  var star = stars[i];
	 	  star.observe('mouseover', rating.__onMouseOver.bindAsEventListener(star), false );
	 	  star.observe('mouseout', rating.__onMouseOut.bindAsEventListener(star), false );
	 	  star.observe('click', rating.__onClicked.bindAsEventListener(star), false );
	 	}
 	}
 }
 
 ,__getStarRating: function(dom){
 	return parseInt( dom.id.charAt(4) ); //starX
 }
 
 ,__showRating: function(ratingValue){
 	var stars = $$('.rating .stars .star');
 	for( var i=0; i<stars.length; i++ ){
 	  var star = stars[i];
 	  var starRating = rating.__getStarRating(star);
 	  var starClass = 'emptystar';
 	  if( starRating <= ratingValue ) starClass = 'fullstar';
 	  else if( starRating <= ratingValue + 0.5 ) starClass = 'halfstar';
 	  var starClass = 'star ' + starClass;
 	  if( starClass!=star.className ) star.className = starClass; 	  
	}
 }
 
 ,__onMouseOver: function(ev){
 	var newRating = rating.__getStarRating(this);
 	rating.__showRating( newRating );
 	
 	$('explaination_star' + newRating ).style.display='block';
 	rating.isHover = true;
 }

 ,__onMouseOut: function(ev){
    rating.isHover = false;
 	$('explaination_star' + rating.__getStarRating(this) ).style.display='none';
    window.setTimeout( function() {
    	if( !rating.isHover ) {
 			rating.__showRating( rating.oldRating );
 		}
 	}, 1 );
 }
 
 ,__onClicked: function(ev){ 
 	if( rating.isActive ){
		Event.stop(ev);
		return false;
 	}
 	var newRating = rating.__getStarRating(this);

	  if( !empty(rating.ajaxUrl) ){ 	
 	  	rating.isActive = true;
		var ajaxData = {
			method: 'post', 
			postBody: 'rating='+newRating, 
			onSuccess: rating.__onAjaxSuccess.bind(this), 
			onFailure: rating.__onAjaxFailure.bind(this)
		};
		new Ajax.Request( rating.ajaxUrl, ajaxData );
		
		Event.stop (ev);
		return false;
 	}
 }

 ,__onAjaxSuccess: function(req){
 	rating.isActive = false;
 	var productRating = $('productRating');
 	if( null!=productRating ){
		var oldDiv = productRating;
		var newDiv = document.createElement(oldDiv.tagName);
		
		newDiv.id = oldDiv.id;
		newDiv.className = oldDiv.className;
		newDiv.innerHTML = req.responseText;
		
		oldDiv.parentNode.replaceChild(newDiv, oldDiv);
 	}
 }
 
 ,__onAjaxFailure: function(req){
 	rating.isActive = false;
 }
};


/* restrict the textarea send_as_ecard and ask_a_friend to 3 Rows and the 3.Row to max. 90 Chars */
var countLines = {
	__mod: function () {
		var areaText = new Array();
		var areaCounter = 0;	
		var message = arguments[1];
		var area = document.getElementById( message );			
		var Ergebnis = area.value.match(/\n/gi);
		areaText = area.value;
		areaCounter = areaText.search(/\n.+$/);
		if(Ergebnis) {
			if( Ergebnis.length == 2 && areaCounter != -1 ) {
				document.getElementById( message ).value = areaText.substring( 0, areaCounter + 90 );
			}
			if(Ergebnis.length > 2){			
				if( Prototype.Browser.IE ) {
					areaText = areaText.substr(0,areaText.search(/\n$/)-1);
				}else{
					areaText = areaText.substr(0,areaText.search(/\n$/));
				}
				document.getElementById( message ).value = areaText ;
			}
		}
	}
}

var send_as_ecard = {
 init: function(){
 	var links = ['send_as_ecard', 'ecard_valid', 'ecard_sent'];
 	for( var i=0; i<links.length; i++ ) {
	 	var link = $(links[i]);
	 	if( null!=link ){
	 		link.observe('click', send_as_ecard.__onLinkClicked.bindAsEventListener(link), false );
	 	}
 	}
 	
 	var url = DOM_getText( $('showSetUrl') );
 	var shows = $$('#ecardSent #boxClose');
 	if( !empty(url) ) for( var i=0; i<shows.length; i++ ) shows[i].href = url;
 }
 
 ,__onLinkClicked: function( ev ){
 	var ajaxUrl = DOM_getText( $(this.id+'_url') );
 	if( empty(ajaxUrl) ) return;
	dialog_layer.requestForm( ajaxUrl, send_as_ecard.__initDialogButtons );
 }

 ,__initDialogButtons: function() {
 	var form = $$('#dialogLayer form');
	var sendButton = $('dwfrm_ecard_send');
	var clearButton = $('dwfrm_ecard_clear');
	var changeMessage = $('dwfrm_ecard_message');
	var ajaxUrl = DOM_getText( $('ecardContinueUrl') );
 	if( !empty(form) && !empty(ajaxUrl) && null!=sendButton && null!=clearButton ){
 		form = form[0];
 		
 		sendButton.observe('click', dialog_layer.formSubmit.bindAsEventListener(form,clearButton,ajaxUrl), false );
 		clearButton.observe('click', dialog_layer.formSubmit.bindAsEventListener(form,sendButton,ajaxUrl), false );
 		changeMessage.observe('keyup', countLines.__mod.bindAsEventListener( this, changeMessage.name ), false );
 	}
 	
 	var anotherButton = $('ecardAnother');
 	if( null!=anotherButton ) {
 		anotherButton.observe( 'click', send_as_ecard.__onLinkClicked.bindAsEventListener(anotherButton), false );
 	}
 }

};


var ask_a_friend = {
 init: function(){
 	var links = ['ask_a_friend', 'email_valid', 'request_sent'];
 	for( var i=0; i<links.length; i++ ) {
	 	var link = $(links[i]);
	 	if( null!=link ){
	 		link.observe('click', ask_a_friend.__onLinkClicked.bindAsEventListener(link), false );
	 	}
 	}
  	var url = DOM_getText( $('showProductUrl') );
 	var shows = $$('#askafriendSent #boxClose');
 	if( !empty(url) ) for( var i=0; i<shows.length; i++ ) shows[i].href = url;
 }
 
 ,__onLinkClicked: function( ev ){
 	var ajaxUrl = DOM_getText( $(this.id + '_url') );
 	if( empty(ajaxUrl) ) return;
	dialog_layer.requestForm( ajaxUrl, ask_a_friend.__initDialogButtons );
 }
 
 ,__initDialogButtons: function() {
 	var form = $$('#dialogLayer form');
	var sendButton = $('dwfrm_askafriend_send');
	var clearButton = $('dwfrm_askafriend_clear');
	var changeMessage = $('dwfrm_askafriend_message');
 	var ajaxUrl = DOM_getText( $('askafriendContinueUrl') );
 	if( !empty(form) && !empty(ajaxUrl) && null!=sendButton && null!=clearButton ){
 		form = form[0];

 		sendButton.observe('click', dialog_layer.formSubmit.bindAsEventListener(form,clearButton,ajaxUrl), false );
 		clearButton.observe('click', dialog_layer.formSubmit.bindAsEventListener(form,sendButton,ajaxUrl), false );
 		changeMessage.observe('keyup', countLines.__mod.bindAsEventListener( this, changeMessage.name ), false );
 	}

 	var anotherButton = $('askafriendAnother');
 	if( null!=anotherButton ) {
 		anotherButton.observe( 'click', ask_a_friend.__onLinkClicked.bindAsEventListener(anotherButton), false );
 	}
 }
};

var dialog_layer = {
 isActive: false
 
 ,requestForm: function( ajaxUrl, initButtonsFunc ){
 	if( dialog_layer.isActive ) return;
	dialog_layer.__initDialogButtons = initButtonsFunc;
	dialog_layer.isActive = true;
	var ajaxData = {
		method: 'post', 
		postBody: '', 
		onSuccess: dialog_layer.__onAjaxSuccess.bind(this), 
		onFailure: dialog_layer.__onAjaxFailure.bind(this)
	};
	new Ajax.Request( ajaxUrl, ajaxData );
 }

 ,formSubmit: function(ev, button, ajaxUrl) {
 	if( dialog_layer.isActive ) return;

	//disable the form
	var parameters = Form.serialize(this,true); //after disable this wont work!
	Form.disable( this );
	delete parameters[button.id];
	
	dialog_layer.isActive = true;
	var ajaxData = {
		method: 'post', 
		parameters: parameters, 
		onSuccess: dialog_layer.__onAjaxSuccess.bind(this), 
		onFailure: dialog_layer.__onAjaxFailure.bind(this)
	};
	new Ajax.Request( ajaxUrl, ajaxData );
 }
  
 ,__onCloseClicked: function( ev ){
 	dialog_layer.isActive = false;
 	var oldDiv = $('dialogLayer');
 	if( null!=oldDiv )
 		Element.extend( oldDiv.parentNode ).remove();
	var actionbox = $('productActions') || $('productSet');
	if( null!=actionbox )
		actionbox.show();
 }
 
 ,__onAjaxSuccess: function( req ){
 	if( !dialog_layer.isActive ) return; //already closed
 	dialog_layer.isActive = false;
 	
 	var description = $('productDescription');
 	if( null==description ) return;
 	
 	var oldDiv = $('dialogLayer');
	var newDiv = document.createElement('div');
	newDiv.innerHTML = req.responseText;

	if( oldDiv )	
		newDiv = oldDiv.parentNode.parentNode.replaceChild(newDiv, oldDiv.parentNode);
	else
		newDiv = description.insertBefore( newDiv, null );
	
	var layer = $('dialogLayer');
	if( null!=layer ){	
 		layer.style.left = (-10)+'px';
 		layer.style.top = (-10)+'px';
 		
 		var actionbox = $('productActions') || $('productSet');
 		if( null!=actionbox )
 			actionbox.hide();
 	}
 	
 	dialog_layer.__initDialogButtons();

 	var showPrivacyPolicy = $('showPrivacyPolicy');
 	var privacyPolicyUrl = DOM_getText( $('privacyPolicyUrl') );
 	if( null!=privacyPolicyUrl && null!=showPrivacyPolicy ){
 		showPrivacyPolicy.observe('click', dialog_layer.__onIBoxLinkClicked.bindAsEventListener(showPrivacyPolicy, privacyPolicyUrl), false );
 	}
 	
 	var closeButtons = $$('#dialogClosebutton, #boxClose');
 	for( var i=0; i< closeButtons.length; i++ )
 		closeButtons[i].observe('click', dialog_layer.__onCloseClicked.bindAsEventListener(closeButtons[i]), false );
 }
 
 ,__onAjaxFailure: function( req ){
 	dialog_layer.__onCloseClicked(null);
 }
 
 ,__onIBoxLinkClicked: function( ev, ajaxUrl ){
	var ajaxData = {
		method: 'get', 
		parameters: '', 
		onSuccess: box_layer.__onAjaxSuccess.bind(this), 
		onFailure: box_layer.__onAjaxFailure.bind(this)
	};
	new Ajax.Request( ajaxUrl, ajaxData );
	Event.stop (ev);
	return false;
 }
};

box_layer = {
 __onAjaxSuccess: function( req ) {
	iBox.show( req.responseText );
 }
 ,__onAjaxFailure: function( req ) {
 }
};

set_image_zoom = {
 init: function() {
 	links = $$('.Set #productImage .zoom');
 	for( var i=0; i < links.length; i++ ) {
 	  var link = links[i];
 	  var urls = link.select( '.zoomImageUrl' );
 	  if( empty(urls) ) continue;
 	  var url = DOM_getText( urls[0] );
 	  if( empty(url) ) continue;
 	  link.observe('click', set_image_zoom.__show.bindAsEventListener( link, url ), false );
 	}
 }
 
 ,__show : function( ev, url ) {
 	iBox.showURL( url );
 } 
}

print_link = {
 init: function() {
 	var link = $('print_link');
 	var print_url = DOM_getText( $('print_url') );
 	if( null!=link && !empty(print_url) ) {
 		link.observe('click', print_link.__onClick.bindAsEventListener( link, print_url ), false );
 	}
 }
 
 ,__onClick: function( ev, url ) {
 	var size = size_selector.current_size['#productActions '];
 	var image = parseInt(image_pager.current_image.charAt(5));
 	var set = DOM_getText( $('productSetId') );
 	var pid = DOM_getText( $('productId') );
 	var params = [];
 	//if( empty(size) ) return;
 	if( !empty(size) ) params.push( 'pid='+size );
 	else if( !empty(pid) ) params.push( 'pid='+pid );
 	else return false;
 	if( !empty(image) ) params.push( 'img='+image );
 	if( !empty(set) ) params.push( 'set='+set );
 	document.location.href = url + '&' + params.join('&');
 	ev.stop();
 	return false;
 }
}