$(document).ready(function(){
	if($('#popup').length > 0){
		$('#popup-screen').fadeTo(0,0);
		$('#popup-screen').delay(400).fadeTo(400,0.75).click(function(){
			$('#popup').remove();
			$('#popup-screen').delay(200).fadeTo(300,0,function(){
				$(this).remove();
			});
		});
	}
	
	$('#social #tw').click(function(){
		share('twitter',window.location,$('title').text(),'');
	});
	$('#social #fb').click(function(){
		share('facebook',window.location,$('title').text(),'');
	});
	
	if (!Modernizr.input.placeholder){
		$('input.text,textarea').each(function(){ 
			$(this).val($(this).attr('placeholder')); 
		});
		$('input.text,textarea').focus(function(){	
			if($(this).val()==$(this).attr('placeholder')){
				$(this).val(''); 
			}
		});
		$('input.text,textarea').blur(function(){
			if($(this).val()==''){ 
				$(this).val($(this).attr('placeholder')); 
			}
		});
		$('form').submit(function(){
			$(this).find('input.text,textarea').each(function(){ 
				if($(this).val()==$(this).attr('placeholder')){
					$(this).val(''); 
				}
			});
		});
	}
		
	$('#femail').click(function(){
		$('#fzip').css('display','block').animate({width:95,opacity:1},150)
	});
	$('#fzip').blur(function(){
		$(this).animate({width:0,opacity:0},150).delay(150).css('display','none')
	});
	if($('#main_nav').length > 0){ setup_menu(); }
	
	if($('#choose_lang').length > 0){ setup_lang(); }
	
	if($('#gallery').length > 0){
		setup_gallery();
	}
	
	$(window).resize(bg_resize);
	setTimeout('bg_resize()', 250);
	if($('.event-img').length > 0){
		$('.event-img').nivoSlider({
			effect: 'fade',
			slices: 6,
			boxCols: 3,
			boxRows: 2,
			animSpeed: 500,
			pauseTime: 6000,
			directionNav: false,
			directionNavHide: false,
			controlNav: false,
			controlNavThumbs: false,
	        controlNavThumbsFromRel: false,
			keyboardNav: false,
			pauseOnHover: false
		});
	}
});

function viewTube(triggerID,movieURL,movieWth,movieHt){
  var embedcode = "<iframe width='" + movieWth +"' height='" + movieHt + "' src='"+movieURL+"?modestbranding=1&rel=0&autohide=1&autoplay=1' frameborder='0' allowfullscreen></iframe>";
  document.getElementById(triggerID).innerHTML = embedcode;
}

function setup_gallery(){
	if($('#gallery ul').length > 0){
		var list = $('#gallery ul').css('margin',0);
		var items = $('#gallery li');
		items.each(function(i,el){
			$(el).find('img').fadeTo(250,1);
			$(el).attr('id','el_'+i);
			var prices = $(el).find('.prices');
			prices.data('initial-height',prices.height());
			var close_price = $('<a href="javascript:void(0)" class="close_price"> x</a>').appendTo(prices);
		});
		
		$('.close_price').click(function(e){
			e.stopPropagation();
			e.preventDefault();
			var prices = $(this).parent('.prices');
			if(prices.width() > 20){
				var w = 0, h = 0;
			}else{
				var w = 120, h = prices.data('initial-height');
			}
			prices.animate({ width: w, height: h },150,function(){
				$(this).find('.close_price').toggleClass('on');
			});
		});
		
		items.click(gotoItem);
		
		$('#el_0').addClass('selected');
		
		var prev = $('<a href="javascript:void(0);" id="prev" class="btn_gallery">prev</a>').appendTo(items.eq(0)).css({
			bottom: (items.eq(0).height() - $('#prev').height())/2
		});
		var next = $('<a href="javascript:void(0);" id="next" class="btn_gallery">next</a>').appendTo(items.eq(0)).css({
			bottom: (items.eq(0).height() - $('#next').height())/2
		});
	/*	var zoom = $('<a href="#zoom" id="zoom" class="btn_gallery">zoom</a>').appendTo(items.eq(0)).css({
			right: 10,
			bottom: 10
		});	*/
			
		$('#prev').unbind('click').bind('click',goPrev);
		$('#next').unbind('click').bind('click',goNext);
	/*	$('#zoom').unbind('click').bind('click',zoomIn);*/
	}else if($('#gallery p#big_img').length > 0){
		
		$('#gallery p#big_img').find('img').fadeTo(250,1);
		
		$(window).mousemove(function(e){
			var stage_h = $('#container').height()-$('#main_ftr').height();
			var img_h = $('p#big_img img').height();
	   	var mouseY = (e.pageY <= stage_h)? e.pageY : stage_h;
			var img_top = (stage_h - img_h)*mouseY/stage_h;
			$('#gallery p#big_img').stop().animate({top:img_top},200);
		});
	}
}

function positionUI(){
	var select_item = $('#gallery li.selected').css('overflow','visible');
	$('#prev,#next,#zoom').each(function(){
			$(this).appendTo(select_item);
			if($(this).attr('id') == 'prev'){
				$(this).animate({
					bottom: (select_item.height() - $(this).height())/2
				},100);
			}
			
			if($(this).attr('id') == 'next'){
				$(this).animate({
					bottom: (select_item.height() - $(this).height())/2
				},100);
			}
			
			if($(this).attr('id') == 'zoom'){
				$(this).animate({
					right: 10,
					bottom: 10
				},100);
			}
	});
}

function goNext(e){
	e.stopPropagation();
	e.preventDefault();
	var req_i = $('li.selected').index('#gallery li') + 1;
	var call_i = (req_i < $('#gallery li').length)? req_i : 0;
	$('#gallery li').eq(call_i).click();
}
function goPrev(e){
	e.stopPropagation();
	e.preventDefault();
	var req_i = $('li.selected').index('#gallery li') - 1;
	var call_i = (req_i >= 0)? req_i : $('#gallery li').length - 1;
	$('#gallery li').eq(call_i).click();
}
function zoomIn(e){
	e.stopPropagation();
	e.preventDefault();
	/*
	$(this).addClass('active');
	var url = $('li.selected').find('a.large').attr('href');
	var big = $('<p id="big_img" />').prependTo('#gallery');
	var img = $('<img src="'+url+'" />').appendTo(big);
	img.bind('load',function(){
		$(this).fadeTo(250,1);
	});
	
	var arr_up = $('<div id="arrow_up"><img src="/_html5ize/img/arrow_up.png" /></div>').appendTo('#gallery').css({
			'position':'fixed',
			'top':0,'left':0,'right':0,
			'padding':20,
			'textAlign':'center',
			'zIndex':290
		}).fadeTo(0,0);
		
	var pos_b = ($(window).height()>640)? 40 : 0;
	var arr_down = $('<div id="arrow_down"><img src="/_html5ize/img/arrow_down.png" /></div>').appendTo('#gallery').css({
		'position':'fixed',
		'bottom':pos_b,'left':0,'right':0,
		'padding':20,
		'textAlign':'center',
		'zIndex':290
	}).fadeTo(0,0);
	arr_down.fadeTo(300,0.5);
	arr_up.fadeTo(300,0.5);
	
	var leave = function(e){
		e.preventDefault();
		arr_down.fadeTo(300,0.5);
		arr_up.fadeTo(300,0.5);
	}
	arr_up.bind('mouseleave',leave);
	arr_down.bind('mouseleave',leave);
	
	arr_up.bind('mouseenter',function(e){
		e.preventDefault();
		$(this).fadeTo(300,1);
		arr_down.fadeTo(300,0.15);
	});
	arr_down.bind('mouseenter',function(e){
		e.preventDefault();
		$(this).fadeTo(300,1);
		arr_up.fadeTo(300,0.15);
	});
	
	$(document).bind('mousemove',function(e){
		var stage_h = ($(window).height()>640)? $(window).height()-$('#main_ftr').height() : $(window).height();
		var img_h = big.find('img').height();
	  var mouseY = (e.pageY <= stage_h)? e.pageY : stage_h;
		var img_top = (stage_h - img_h)*mouseY/stage_h;
		big.stop().animate({top:img_top},200);
	});
	
	img.click(function(){
		$(document).unbind('mousemove');
		$("#zoom").removeClass('active');
		$(this).fadeTo(250,0);
		$(this).parent('p').remove();
		$('#arrow_down,#arrow_up').remove();
	});
	*/
}


function gotoItem(e){
		e.stopPropagation();
		e.preventDefault();
		var prev_sel = $('#gallery li.selected').index('#gallery li');
		var curr_sel = $(this).index('#gallery li');
		$('#gallery li.selected').removeClass('selected');
		var item = $(this).addClass('selected');
		var list = $('#gallery ul');
		positionUI();
		var new_left = list.data('h1Width') + (list.data('mainZone') - item.width())/2 - item.position().left;
		
		list.stop().animate({left:new_left},650,function(){
			if(curr_sel > prev_sel){
				var moved = $('#gallery li').first().width();
				var pos = new_left + moved;
				$('#gallery li').first().appendTo('#gallery ul');
				$('#gallery ul').css({left:pos});
			}
			else{
				var moved = $('#gallery li').last().width();
				var pos = new_left - moved;
				$('#gallery li').last().prependTo('#gallery ul');
				$('#gallery ul').css({left: pos});
			}
		});
}

function setup_lang(){
	if(!Modernizr.csstransitions){
		$('#choose_lang li a').mouseenter(function(){
			$(this).stop().animate({width:84},250);
		});
		$('#choose_lang li a').mouseleave(function(){
			$(this).stop().animate({width:56},250);
		});
	}
}
function bg_resize(){
	if($(window).height()>640 && $('body').attr('id') != 'Campagnes_vid'){
		$('#main_ftr').css({'position':'fixed','bottom':0});
		$('#container').height($(window).height());
		if($('#main').length > 0 && $('#main').outerHeight() > $(window).height()){
			$('#container').height($('#main').outerHeight());
		}
		else{
			$('#container').height($(window).height());
		}
	}
	else{
		$('#main_ftr').css({'position':'absolute','bottom':0});
		if($('#main').length > 0 && $('#main').outerHeight() > 640){
			$('#container').height($('#main').outerHeight());
		}
		else{
			$('#container').height(640);
		}
	}

	if($(window).width()>960){
		$('#container').width($(window).width());
	}
	else{
		$('#container').width(960);
	}
	$('#bgs').width($(window).width()).height($(window).height());
	$('#bgpopup').width($(window).width()).height($(window).height());
	$('.background').each(function(){
		var bg = $(this);
		var win = $(window);
		var w_ratio = win.width()/win.height();
		var bg_ratio = bg.width()/bg.height();
		if(w_ratio>bg_ratio){
			var bg_width = win.width();
			var bg_height = win.width()/bg_ratio;
		}else{	
			var bg_width = win.height()*bg_ratio;
			var bg_height = win.height();
		}
		bg.width(bg_width);
		bg.height(bg_height);
		bg.css(
			{'marginTop':Number((win.height()-bg_height)/2),'marginRighMap.t':Number((win.width()-bg_width)/2)}
		)
	});
	
	if($('#gallery').length > 0){
		var list = $('#gallery ul');
		var items = $('#gallery li');
		var count = items.length;
		items.each(function(i,el){
			var anch = $(el).find('a.large');
			var img = anch.find('img');
			var img_ratio = img.data('init-width') / img.data('init-height');
			img.width(($('#container').height()-$("#main_ftr").outerHeight()) * img_ratio);
			img.height($('#container').height()-$("#main_ftr").outerHeight());
			
			anch.width(img.width());
			$(el).width(anch.outerWidth());
			if (!--count) positionUI();
		});
		positionUI();
		list.data('h1Width',$('h1').width());
		list.data('mainZone',$('#container').width() - ($('h1').width() + 20+$('#right_nav').width()));
		
		$('#gallery li.selected').click();
	}
	
	if($('#map_stores').length > 0){
		$('#map_stores').width($('#container').width()).height($('#container').height()-40);
		initialize();
	}
}

function setup_menu(){
	$('#main_nav ul.sub').slideUp(0);
	$('#main_nav .active ul.sub').slideDown(0);

	$('#main_nav li.menu').mouseenter(function(){
		$(this).addClass('hover');
	});
	$('#main_nav li.menu').mouseleave(function(){
		$(this).removeClass('hover');
	});
	$('#main_nav li.menu>a').click(function(){
		if(!$(this).parent('li').hasClass('active')){
			$('#main_nav li.active').find('ul.sub').slideUp(125);
			$('#main_nav li.active').removeClass('active');
			$(this).parent('li').addClass('active');
			$(this).parent('li').find('ul.sub').slideDown(175);
		}else{
			$('#main_nav li.active').find('ul.sub').slideUp(125);
			$('#main_nav li.active').removeClass('active');
		}
	});
}
