// JavaScript Document
var scrollApi;
var scrollApi_news;
var image_ratios = new Array();
var IE = document.all?true:false;
var mouse_x = 0;
var mouse_y = 0; 
var hover_timer;
var content_height=0;
$(function(){
	$('#index_image_container').click(function(){
		hide_index_image();
	});
	
	var current_section = $('#current_section').val();
	$('.current_section').html(current_section);
	$('#menu .submenu li').each(function(){
		if($('a',this).html()==current_section){
			$(this).addClass('current');
		}
	});
	
	/*
	$('#menu .current_section').hover(
		function(){
			$(this).stop(true,true).hide();
			$('#menu .submenu').stop(true,true).fadeIn();
		},
		function(){
		}
	);
	$('#menu').hover(
		function(){
		},
		function(){
			$('.current_section',this).stop(true,true).fadeIn();
			$('.submenu',this).stop(true,true).hide();
		}
	);
	*/
	$('#footer_menu .twitter, #footer_menu .facebook, #footer_menu .mail').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	setTimeout("hide_index_image();",8000);
	
	//COLLECTIONS
	if($('#collections_slider_container').size()>0){
		$('#collections_slider .item').each(function(index){
			image_ratios[index]=$('img',this).width()/$('img',this).height();
		});	
		$('#collections_slider .item img').hover(
			function(){
				$(this).closest('.item').find('.hover_legend').stop(true,true).fadeIn();
			},
			function(){
				$(this).closest('.item').find('.hover_legend').stop(true,true).fadeOut();
			}
		);
		$('#collections_slider_container').jScrollPane({'autoReinitialise':true,'showArrows':false,'hideFocus':true});
		scrollApi = $('#collections_slider_container').data('jsp');
	}
	//SELECTED COLLECTION
	if($('#collection_info_container').size()>0){
		$('#collection_info_container #small_image').each(function(index){
			image_ratios[index]=$('img',this).width()/$('img',this).height();
		});	

		$('.views_mark .url_small').each(function(i){
			if(i>0){
				var img=$('<img/>').addClass('views_m').attr("src",$(this).val()).hide().appendTo('#collection_info_container #small_image');
			}
		});
		$('.views_mark .url').each(function(i){
			if(i>0){
				var img=$('<img/>').attr("src",$(this).val()).hide().appendTo('#super_zoom');
			}
		});
		$('.views_mark').click(function(){
			if(!$(this).is('.current')){
				var index=$(this).index('.views_mark');
				$('.views_mark.current').removeClass('current');
				$(this).addClass('current');
				var src = $('.views_mark.current .url').val();
				var src_small = $('.views_mark.current .url_small').val();
				var width = $('.views_mark.current .width').val();
				var height = $('.views_mark.current .height').val();
				image_ratios[0]=width/height;
				$('#collection_info_container #small_image img').hide();
				$('#collection_info_container #small_image img.views_m:eq('+index+')').stop(true,true).fadeIn();
				$('#super_zoom img').hide();
				$('#super_zoom img:eq('+index+')').stop(true,true).fadeIn();
				resize();
			}
		});
		
		$('.index_mark .url_small').each(function(i){
				var img=$('<img/>').addClass('index_m').attr("src",$(this).val()).hide().appendTo('#collection_info_container #small_image');;
		});
		$('.index_mark').hover(
			function(){
				clearTimeout(hover_timer);
				var index=$(this).index('.index_mark');
				$('#collection_info_container #small_image img').hide();
				$('#collection_info_container #small_image img.index_m:eq('+index+')').stop(true,true).fadeIn();
				
				var src = $('.url_small',this).val();
				var width = $('.width',this).val();
				var height = $('.height',this).val();
				image_ratios[0]=width/height;
				//resize();
			},
			function(){
				hover_timer=setTimeout('reset_hover();',500);
			}
		);
		
	}
	//SUPERZOOM
	if($('#super_zoom').size()>0){
		var index = $('#super_zoom img:visible').index('#super_zoom img');
		var ratio=image_ratios[index];
		width = $(window).width()-$('#collection_info_container').width();
		//console.log(width);
		var i_height = width/ratio;
		var w_height=$(window).height();
		//var margin_limit=i_height-w_height;
		var margin_limit=0;
		var margin=margin_limit*(0.5);
		var offset=$('#super_zoom').offset();
		$('#super_zoom img:visible').css({'margin-top':-margin});

		if (!IE){
			document.captureEvents(Event.MOUSEMOVE);
		}
		document.onmousemove = getMouseXY;
		var timer=setInterval("super_zoom();",100);
	}
	//ABOUT
	if($('#about_container').size()>0){
		content_height=$('#about_container .content').height();
		if(content_height>$(window).height()-270){
			var new_height=$(window).height()-270;
			$('#about_container .content').height(new_height);
		}

		$('#about_container .content').jScrollPane({'showArrows':false,'hideFocus':true});
		scrollApi = $('#about_container .content').data('jsp');
	}
	//STOCKISTS
	if($('#stockists_container').size()>0){
		var new_height=$(window).height()-300;
		$('#stockists_container .content').height(new_height+20);
		//console.log(new_height);
		var top=50;
		var left=0;
		$('.stockist').each(function(index){
			$(this).css({'top':top,'left':left}).show();
			$('hr').parent().width(left+200);
			if(top+$(this).height()+50>=new_height-30){
				left=left+$(this).width()+65;
				top=50;
			}else{
				top=top+$(this).height()+30;
			}
		});
		$('#stockists_container .content').jScrollPane({'showArrows':false,'hideFocus':true});
		scrollApi = $('#stockists_container .content').data('jsp');
	}
	//LEGAL
	if($('#legal_container').size()>0){
		var new_height=$(window).height()-310;
		var mod=new_height%15;
		var legal_height=$('.hidden_text').height();
		$('.hidden_text').hide();
		var column_count = Math.ceil(legal_height/new_height);

		$('#legal_container .content').height(new_height+47);
		$('#legal_container .legal_content').height(new_height);
		$('#legal_container').width($(window).width()-160);
		
		table=$('<table/>').append('<tr></tr>');
		for(i=0;i<column_count;i++){
			col_content=$('<div/>').addClass('col_content').css({'position':'absolute','top':-(new_height-mod-15)*i}).html($('.hidden_text').html());
			td=$('<td width="320px" height="'+new_height+'px" valign="top"><div class="legal_column"></div></td>');
			$(td).find('.legal_column').append($(col_content).clone());
			$(table).find('tr').append($(td).clone());
			//$('.legal_column:eq('+i+') .col_content').css({'margin-top':-(new_height-20)*i});
		}
		$('#legal_container .hidden_text').hide();
		$('#legal_container .legal_content').html(table);
		$('#legal_container .legal_column').height(new_height-mod-16);

		$('#legal_container .legal_content').jScrollPane({'showArrows':false,'hideFocus':true});
		scrollApi = $('#legal_container .legal_content').data('jsp');
	}
	//NEWS
	if($('#news_container').size()>0){
		var new_height=$(window).height()-240;
		if(new_height>360){
			new_height=360;
		}
		$('#news_container .news_index').height(new_height+20);
		
		$('#news_container .news_index').jScrollPane({'showArrows':false,'hideFocus':true});
		scrollApi = $('#news_container .news_index').data('jsp');
		

		var new_height=$(window).height()-240;
		if(new_height<494){
			$('#news_container .news_briefings').height(new_height);
		}
		$('#news_container .news_briefings').jScrollPane({'showArrows':false,'hideFocus':true});
		scrollApi_news = $('#news_container .news_briefings').data('jsp');

		$('#news_container .news_index .index').click(function(){
			if(!$(this).is('.current')){
				$('.news_index .index.current').removeClass('current');
				$(this).addClass('current');
				
				var index=$(this).index('.news_index .index');
				var margin_top=-(index*164);
				$('.news_briefings .briefing.current').removeClass('current');
				$('.news_briefings .briefing:eq('+index+')').addClass('current');
				$('#news_slide').animate({'margin-top':margin_top},500);
			}
		});
	}
	//PRESS
	if($('#press_info_container').size()>0){
		/*
		$('.pages_mark').each(function(){
			$('<img/>').attr('src',$(this).find('.url_small').val()).appendTo('#press_thumbs');
		});
		*/
		$('.pages_mark').click(function(){
			var index=$(this).index('.pages_mark');
			$('.pages_mark.current').removeClass('current');
			$(this).addClass('current');
			$('.pages_mark:eq('+(index+1)+')').addClass('current');
			var margin=-(367*index);
			$('#press_slider').animate({'margin-left':margin},700);
		});
		/*
		$('#press_thumbs img').click(function(){
			if(!$(this).is('.current')){
				var index=$(this).index('#press_thumbs img');
				$('.pages_mark.current').removeClass('current');
				$('.pages_mark:eq('+index+')').addClass('current');
				var margin=-(367*index);
				$('#press_slider').animate({'margin-left':margin},700);
			}
		});
		*/
		
		var new_height=$(window).height()-180;
		$('#press_container').height(new_height);
		$('#press_thumbs').jScrollPane({'showArrows':false,'hideFocus':true});
		$('#press_container').jScrollPane({'showArrows':false,'hideFocus':true});
		scrollApi = $('#press_container').data('jsp');
	}
	
	
	$('#newsletter_container #newsletter_fader,#newsletter_container .close_button').click(function(){
		hide_newsletter();
	});
	$('#video_container #video_fader,#video_container .close_button').click(function(){
		hide_video();
	});
	$('#email_container #email_fader,#email_container .close_button').click(function(){
		hide_email_form();
	});
	
	resize();
	$(window).resize(function(){
		resize();	
	});
});

function resize(){
	var ratio = $('#super_image img').width()/$('#super_image img').height();
	var win_ratio = $(window).width()/$(window).height();
	//console.log('resize');
	
	if(ratio>win_ratio){
		$('#super_image img').height($(window).height());
		$('#super_image img').width(ratio*$(window).height());
	}
	else{
		$('#super_image img').width($(window).width());
		$('#super_image img').height($(window).width()/ratio);
	}
	
	//COLLECTIONS
	if($('#collections_slider_container').size()>0){
		//console.log('COLLECTIONS');
		var new_height=$(window).height()-226;
		$('#collections_slider_container').height(new_height+20);
		var width=0;
		var padding=0;
		$('#collections_slider .item').each(function(index){
			var ratio=image_ratios[index];
			$('img',this).height(new_height-7);
			$('img',this).width(ratio*(new_height-7));
			
			padding=$(window).width()*75/1280;
			$(this).css({'padding':'0 '+padding+'px'})
			width+=$(this).outerWidth();
		});

		$('#collections_slider').width(width+padding*2);
		scrollApi.reinitialise();
	}
	//SELECTED COLLECTION
	if($('#collection_info_container').size()>0){
		//console.log('SELECTED COLLECTION');
		var new_height=$(window).height()-200;
		$('#collection_info_container').height(new_height+20);
		var width=0;
		$('#collection_info_container #small_image img').each(function(index){
			var ratio=image_ratios[index];
			$(this).height(new_height);
			$(this).width(ratio*new_height);
		});
		var index = $('#super_zoom img:visible').index('#super_zoom img');
		var ratio=image_ratios[index];
		width = $(window).width()-$('#collection_info_container').width();
		//console.log(width);
		$('#super_zoom').width(width);
		$('#super_zoom').height(width/ratio);
	}
	//SUPER ZOOM
	if($('#super_zoom').size()>0){
		$('#super_zoom .legend').height($(window).height());
		//console.log('SUPER ZOOM');
		if($(window).width()>1200){
			//$('#super_zoom').css({left:($(window).width()-1200)/2});
			//$('#collection_info_container').css({'margin-right':($(window).width()-1200)/2});
		}
	}
	//ABOUT
	if($('#about_container').size()>0){
		//console.log('ABOUT');
		if(content_height>$(window).height()-250){
			var new_height=$(window).height()-250;
			$('#about_container .content').height(new_height);
		}
		else{
			$('#about_container .content').height(content_height+2);
		}
		scrollApi.reinitialise();
	}
	//STOCKISTS
	if($('#stockists_container').size()>0){
		var new_height=$(window).height()-280;
		$('#stockists_container .content').height(new_height+20);
		
		scrollApi.reinitialise();
	}
	//LEGAL
	if($('#legal_container').size()>0){
		//console.log('ABOUT');
		var new_height=$(window).height()-320;
		$('#legal_container .content').height(new_height+47);
		
		scrollApi.reinitialise();
	}
	//NEWS
	if($('#news_container').size()>0){
		//console.log('ABOUT');
		var new_height=$(window).height()-240;
		if(new_height>360){
			new_height=360;
		}
		$('#news_container .news_index').height(new_height+20);
		
		new_height=$(window).height()-240;
		if(new_height>494){
			new_height=494;
		}
		$('#news_container .news_briefings').height(new_height);
		
		scrollApi.reinitialise();
		scrollApi_news.reinitialise({});
	}
	
	//PRESS
	if($('#press_container').size()>0){
		var new_height=$(window).height()-180;
		$('#press_container').height(new_height);
		scrollApi.reinitialise();
	}
}

function hide_index_image(){
	$("#index_image_container").fadeOut();
}

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouse_x = event.clientX + document.body.scrollLeft
    mouse_y = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouse_x = e.pageX
    mouse_y = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouse_x < 0){mouse_x = 0}
  if (mouse_y < 0){mouse_y = 0}  
}

function super_zoom(){
	var w_height=$(window).height();
	var i_height=$('#super_zoom img:visible').height();
	var margin_limit=i_height-w_height;
	var margin=margin_limit*(mouse_y/w_height);
	var offset=$('#super_zoom').offset();
	if(mouse_x>offset.left && mouse_x<(parseInt(offset.left)+$('#super_zoom').width())){
		$('#super_zoom img:visible').stop(true,true).animate({'margin-top':-margin},300);
		if($('#super_zoom .legend').is(':visible')){
			$('#super_zoom .legend').delay(1500).fadeOut();
		}
	}
}

function show_newsletter(){
	$('#newsletter_container').fadeIn();
}
function hide_newsletter(){
	$('#newsletter_container').fadeOut();
}

function display_video(){
	$('#video_container #video_content').html($('#video_url').html());
	$('#video_container').fadeIn();
}
function hide_video(){
	$('#video_container').fadeOut(function(){
		$('#video_container #video_content').html('');
	});
}

function show_email_form(){
	src=$('#small_image img:visible').attr('src');
	$('#email_container .image img').attr('src',src);
	$('#email_container').fadeIn();
}
function hide_email_form(){
	$('#email_container').fadeOut();
}

function reset_hover(){
	var index=$('.views_mark.current').index('.views_mark');
	$('#collection_info_container #small_image img').hide();
	$('#collection_info_container #small_image img:eq('+index+')').stop(true,true).fadeIn();
	
	var src = $('.views_mark.current .url_small').val();
	var width = $('.views_mark.current .width').val();
	var height = $('.views_mark.current .height').val();
	image_ratios[0]=width/height;
	resize();
}

function subscribe_newsletter(){
	if(!isValidEmailAddress($('#newsletter_form input[name=email]').val())){
		alert('This is not a valid email address.')
		$('#newsletter_form input[name=email]').focus();
		return false;
	}
	if($('#newsletter_form #country').val()==''){
		alert('Please select your country.')
		$('#newsletter_form #country').focus();
		return false;
	}
	if(!$('#accept_reg').is(':checked')){
		alert('You must accept the Newsletter conditions first.')
		return false;
	}
	name=$('#newsletter_form input[name=name]').val();
	email=$('#newsletter_form input[name=email]').val();
	country=$('#newsletter_form #country option:selected').html();

	$.ajax({  url: "includes/subscribe_newsletter.php", 
		  type: "POST",
		  async: false,
		  data: "name="+name+"&email="+email+"&country="+country,
		  success: function(response){
			//console.log(response);
			$('#newsletter_container').fadeOut();
		  }					
	   });
	return true;
}

function send_mail(){
	if($('#enquiry_form input[name=name]').val()=='Name'){
		alert('Please, wite your name.')
		$('#enquiry_form input[name=name]').focus();
		return false;
	}
	if(!isValidEmailAddress($('#enquiry_form input[name=email]').val())){
		alert('This is not a valid email address.')
		$('#enquiry_form input[name=email]').focus();
		return false;
	}
	if($('#enquiry_form textarea[name=text]').val()==''){
		alert('Please, write a message.')
		$('#enquiry_form textarea[name=text]').focus();
		return false;
	}
	name=$('#enquiry_form input[name=name]').val();
	email=$('#enquiry_form input[name=email]').val();
	text=$('#enquiry_form textarea[name=text]').val();

	$.ajax({  url: "includes/send_mail.php", 
		  type: "POST",
		  async: false,
		  data: "name="+name+"&email="+email+"&text="+text,
		  success: function(response){
			$('#enquiry_form').closest('.content').find('.message').fadeIn().delay(2500).fadeOut();
		  }					
	   });
	return false;
}

function send_friend_email(){
	if($('#email_form input[name=r_name]').val()=='Recipient\'s Name*'){
		alert('Please, wite your friend\'s name.')
		$('#enquiry_form input[name=r_name]').focus();
		return false;
	}
	if(!isValidEmailAddress($('#email_form input[name=r_email]').val())){
		alert('Your friend\'s email is not a valid email address.')
		$('#email_form input[name=r_email]').focus();
		return false;
	}
	if($('#email_form input[name=name]').val()=='Name*'){
		alert('Please, wite your name.')
		$('#email_form input[name=name]').focus();
		return false;
	}
	if(!isValidEmailAddress($('#email_form input[name=email]').val())){
		alert('Your email is not a valid email address.')
		$('#email_form input[name=email]').focus();
		return false;
	}
	if($('#email_form input[name=subject]').val()=='Subject*'){
		alert('Please, wite a subject.')
		$('#email_form input[name=subject]').focus();
		return false;
	}
	if($('#email_form textarea[name=message]').val()==''){
		alert('Please, write a message.')
		$('#email_form textarea[name=text]').focus();
		return false;
	}

	r_name=$('#email_form input[name=r_name]').val();
	r_email=$('#email_form input[name=r_email]').val();
	name=$('#email_form input[name=name]').val();
	email=$('#email_form input[name=email]').val();
	subject=$('#email_form input[name=subject]').val();
	message=$('#email_form textarea[name=message]').val();
	image=$('#email_container .image img').attr('src');
	current_url=$('#email_container input[name=current_url]').val();

	//console.log(r_name+' - '+r_email+' - '+name+' - '+email+' - '+subject+' - '+message+' - '+image);
	$.ajax({  url: "includes/send_friends_mail.php", 
		  type: "POST",
		  async: false,
		  data: "r_name="+r_name+"&r_email="+r_email+"&name="+name+"&email="+email+"&subject="+subject+"&message="+message+'&image='+image+'&current_url='+current_url,
		  success: function(response){
			//console.log(response);
			$('#email_container').fadeOut();
		  }					
	   });
	return true;
}

function isValidEmailAddress(emailAddress) {
 		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 		return pattern.test(emailAddress);
}

