/* global variables */
var cpt_max = 0;
var cpt = 0;

/* functions */



function display_product_menu() {
  product_menu = $('products_drop_down');
  
  if (product_menu.style.display == "none") {
    product_menu.style.display = "block";
  } else {
    product_menu.style.display = "none";
  }
}

// 
// Change the background color of any element
// 	args		element		element you want to change the background color
// 					color			color the new background color
// 	return	nil
//
function changebg(element,color){
	if(element.className!="selected_news"){
		element.setStyle('background',color)
	}
}

// 
// Select a particular item of a collection by changing its class
// 	args		collection			collection of the selected item
// 					selected_item		the selected_item
// 					class_name			class name to apply
// 	return	NULL
// 
function select_item(collection,selected_item,class_name){
	collection.each(function(item){
		item.className = ''
		item.style.background = 'transparent'
	})
	selected_item.className = class_name
	selected_item.setStyle('background','#cccccc')
}

function resize_wrapper() {
  // var wrapper = $("content_wrapper");
  // if (self.innerHeight != undefined) {
  //   if (wrapper.offsetHeight < self.innerHeight) {
  //     wrapper.style.height = self.innerHeight + "px"; 
  //   }
  // } else if (wrapper.offsetHeight < document.documentElement.clientHeight) {
  //   wrapper.style.height = document.documentElement.clientHeight + "px";
  // }
}


function init(){
  resize_wrapper();

  if($('history_wrap')){
		/* #INNER width */
		var inner = $('inner');
		var inner_elements_list = $$('#inner div.history_item');
		var year_bar = $('year_overline')
		year_bar_margin_left = parseInt(year_bar.getStyle('margin-left'))
		year_bar_width = parseInt(year_bar.getStyle('width'))+2


		var width = 780;
		cpt_max = parseInt(inner_elements_list.length)-1;
		inner_width = parseInt(width)*(parseInt(inner_elements_list.length)+1);
		inner.style.width = inner_width+"px";
		
		$('right').effect('opacity').start(0,1)
		$('left').effect('opacity').start(0,1)

		/* SCROLL ACTIONS */
		var scroll = new Fx.Scroll('history_wrap', {
			wait: false,
			duration: 500,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Quad.easeInOut
		});

		$('right').addEvent('click', function(event) {
			event = new Event(event).stop();
			cpt = cpt + 1;
			if(cpt>cpt_max) cpt = 0
			scroll.scrollTo(791*cpt);
			
			year_bar.effect('margin-left').start(year_bar_margin_left+cpt*(year_bar_width+7))
			
		});

		$('left').addEvent('click', function(event) { 
			event = new Event(event).stop();
			if(cpt==0) {
				cpt=cpt_max;
			} else {
				cpt = cpt - 1;
			}
			scroll.scrollTo(791*cpt);
			year_bar.effect('margin-left').start(year_bar_margin_left+cpt*(year_bar_width+7))
		});

    var year_links = $$('#year_links a');

    year_links.each(function(year) {
      year.addEvent('click', function(event) {
        event = new Event(event).stop();
        var our_id = year.id;
        var digits = our_id.substring(our_id.length,our_id.length-2);
        scroll.toElement('history_'+digits);
        cpt = parseInt(digits,10)-1;
				year_bar.effect('margin-left').start(year_bar_margin_left+cpt*(year_bar_width+7))
      })
    })
	}

	if($('scroll_wrap')){
		/* #INNER width */
		var inner = $('inner');
		var inner_elements_list = $$('#inner div.description');
		img = $('new_product_main_image');
		if(img.height){
			img_size = parseInt(img.height);
		}else if(img.style.height){
			img_size = parseInt(img.style.height);				
		}else{
			img_size = 400;
		}
		$('new_product_wrapper').style.height = img_size+"px";

		var width = 250;
		cpt_max = parseInt(inner_elements_list.length)-3;
		inner_width = parseInt(width)*(parseInt(inner_elements_list.length)+1);
		inner.style.width = inner_width+"px";
    
		/* SCROLL ACTIONS */
		var scroll = new Fx.Scroll('scroll_wrap', {
			wait: false,
			duration: 500,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Quad.easeInOut
		});

		if(inner_elements_list.length>3){
			$('right').effect('opacity').start(0,1)
			$('left').effect('opacity').start(0,1)
			$('see_all_bt').effect('opacity').start(0,1)
			
			
			$('right').addEvent('click', function(event) {
				event = new Event(event).stop();
				cpt = cpt + 1;
				if(cpt>cpt_max) cpt = 0
				scroll.scrollTo(261*cpt);
			});

			$('left').addEvent('click', function(event) {
				event = new Event(event).stop();
				if(cpt==0){
					cpt=cpt_max;
				}else{
					cpt = cpt - 1;
				}
				scroll.scrollTo(261*cpt);
			});

		/* CHANGE VIEW */
		var window_scroll = new Fx.Scroll(window, {
			wait: false,
			duration: 400,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Expo.easeIn
		});
		to_height = Math.ceil(parseInt(inner_elements_list.length)/3)*300;
		var myelements = new Fx.Elements(['right','left','new_product_wrapper','inner','scroll_wrap','scroll_bt','see_all_bt']);

		/* ----- see all products ----- */
		
		$('see_all_bt').addEvent('click',function(event){
			event = new Event(event).stop();
			window_scroll.toTop();
			scroll.scrollTo(0);
			setTimeout(function(){
				see_all(scroll,myelements,to_height);
			},500);
		 },{duration:1000});
		
		/* ----- see scroll bar ------ */

		$('scroll_bt').addEvent('click',function(event){
			event = new Event(event).stop();
			window_scroll.toTop();
			setTimeout(function(){
				scroll_view(scroll,myelements);
			},500);
		},{duration:1000});
		
		}// END OF IF		
	}
}

function see_all(scroll,myelements,to_height){
	myelements.start({
			'0':{
				'opacity':[0]
			},
			'1':{
				'opacity':[0]
			},
			'2':{
				'height':[0]
			},
			'3':{
				'width':[800]
			},
			'4':{
				'height':[to_height]
			},
			'5':{
				'opacity':[1]
			},
			'6':{
				'opacity':[0]
			}
		});
}

function scroll_view(scroll,myelements){
	cpt = 0;
	myelements.start({
		'0':{
			'opacity':[1]
		},
		'1':{
			'opacity':[1]
		},
		'2':{
			'height':[img_size]
		},
		'3':{
			'width':[inner_width]
		},
		'4':{
			'height':[300]
		}	,
		'5':{
			'opacity':[0]
		},
		'6':{
			'opacity':[1]
		}
	});
}


function highlight(elt,css,highlight_color,start_color){
	elt.effect(css,{duration:100}).start(highlight_color).chain(function(){
		elt.effect(css,{duration:100}).start(start_color)
	})
}


function init_menu(){
	var timer
	var menu = $('section_navigation')
	var selected_submenu = menu.getElementsBySelector(".select")[0]
	var sections = menu.getChildren()
	
	var menu_fun = function(tab,subsection){
		tab.each(function(li){
			li.removeClass('select')
			li.setStyle('display','none')
		})
		if(subsection){
			subsection.setStyle('display','block')
			subsection.addClass('select')							
		}
	}
	
	// var show_selected_menu = function(have_selected_menu){
	// 	$$('.select').each(function(li){
	// 		li.removeClass('select')
	// 		li.setStyle('display','none')
	// 	})
	// 	if(have_selected_menu){
	// 		selected_submenu.setStyle('display','block')
	// 		selected_submenu.addClass("select")			
	// 	}
	// }
	
	sections.each(function(section){
		var subsection = section.getElement('ul')
		section.addEvents({
			mouseover:function(e){
				timer = menu_fun.delay(200,this,[$$('.select'),subsection])
			},
			mouseout:function(e){
				timer = $clear(timer)
			}
		})
	})
}


/*
	###############
	# NEW PRODUCTS
	###############
*/


function switch_photo_view (display_element_id) {
	$('photo_view_'+display_element_id).setStyle('display','block')
	$('new_product_gallery_'+display_element_id).setStyle('display','block')
	$('model_view_'+display_element_id).setStyle('display','none')
}
function switch_model_view (display_element_id) {
	$('photo_view_'+display_element_id).setStyle('display','none')
	$('model_view_'+display_element_id).setStyle('display','block')
  $('new_product_gallery_'+display_element_id).setStyle('display','none')
}



// 
// Fade out any image and fade in any new image
// 	args		element		element (must be an img tag) you want to change the source for
// 					image			new image we want to appear
//          fade_time duration for fade to last (i.e. for it to fade from image to white)
// 	return	nil
//
function switch_images(element, image, fade_time,size) {
  var image_holder = $(element);
  image = image.toString();

  var new_image = new Image();
  new_image.src = image

  if (image_holder.src != new_image.src) {
		// because fade doesn't work on IE on an image we apply it on the parent.
		// images have to be wrapped in a layer or span
    var myFx = new Fx.Style(image_holder.getParent(), 'opacity',{duration:fade_time});

		// if the image src is /path/blank.gif?..., that means that we are using IE (because of png fix)
		if((/blank.gif/i).test(image_holder.src)){
			// pngfix adds a filter property to the image and applies this filter for transparency
			var new_src = image_holder.getStyle('filter').replace(/(\/)assets(.)+(\.)(png|jpg|gif)((\?)[0-9]+)?/,image)
	    myFx.start(1,0)
			.chain(function() {
      	image_holder.setStyle('filter',new_src)
				myFx.start(0,1)
	    });
		}else{
		  myFx.start(1,0).chain(function() {
	      image_holder.src = image;
	    }).chain(function() {
    		if(size){
    			var width = parseInt(size.split("x")[0])
    			var height = parseInt(size.split("x").getLast())
    			image_holder.setStyle('height',height)
    			image_holder.setStyle('width',width)
    		}	      
	    });
	  	image_holder.addEvent('load', function() {
	  	  myFx.start(0,1)
	  	});	
		}
  }
}



function update_map(location_id){
	var so = new SWFObject("/artwork/design/map.swf", "map", "470", "240", "8", "#ffffff");
  so.addVariable("location",location_id)
	so.write("locations_map")
}