var SITE = SITE || {};

SITE.fileInputs = function() {
  var $this = $(this),
      $val = $this.val(),
      valArray = $val.split('\\'),
      newVal = valArray[valArray.length-1],
      $button = $this.siblings('.button_up'),
      $fakeFile = $this.siblings('.file-holder');
  if(newVal !== '') {
    $button.text('Upload Your CV');
    if($fakeFile.length === 0) {
      $button.after('<span class="file-holder">' + newVal + '</span>');
    } else {
      $fakeFile.text(newVal);
    }
  }
};
$(document).ready(function(){
/***********file custom ************/

$('.file-wrapper input[type=file]').bind('change focus click', SITE.fileInputs);
//cv submit
$('.work_form').click("An email has been sent");
$()

//date calendar
/*$('.calendar').datepicker({
		dateFormat: 'yy-mm-dd',
});*/
//document.oncontextmenu = new Function("return false");
//remove title in all link

$('a').attr('title', '');

	//Reservation selection
	$("[name='venue']").change(function(){
		if($(this).val()=="Restaurant"){
			$("[name='meal']").html("<option>Lunch</option><option>Dinner</option>");	
		}
		if($(this).val()=="Deli"){
		 	$("[name='meal']").html("<option>Dinner</option>");	
		}
		
	});
	//*************get url
	var url = window.location.search;
	
	url = url.replace("?", "");
	url = url.split("&");
	var myUrl = new Array();
	myUrl['m'] = "";
	myUrl['wine'] = "";
	var i = 0;
	while (i < url.length){
		var tmp = url[i].split("=");
		myUrl[tmp[0]] = tmp[1];	
		i++;
	}
	$('#menu_'+myUrl['cat'] + ' a').addClass('cp_theme');
	$('#smenu_'+myUrl['page'] + ' a').addClass('smenu_selected')
/************logo change*************/

if (myUrl['cat'] == "deli"){
	$('#header_logo').css('background-image', 'url(img/template/logo_deli.png)');
	$('#menu_deli a').removeClass('cp_theme');
	$('#menu_deli a').addClass('deli_theme');
	//$('body').supersleight({shim: 'blank.gif'});
}
/*********newsletter delete text******/
$('#newsletter_email').mouseover(function(){
	$(this).val("");
});
$('.submit_newsletter').click(function(){
	var email = $('[name=newsletter_email]').val();
	if (email.length > 3){
			$.ajax({
			type: "POST",
			url: "actions/newsletter.php",
			data: {email:email},
			async: false,
			success: function(msg){
				//alert( "Data Saved: " +msg );
				//alert(form);
				alert("You are now register to the newsletter");
			}
		});
	}
});
/****************load newsletter archive ***************/
$('#archive_newsletter').click(function(){
	$('#archive_choose').load('page/what/newsletter_archive.php', function(){
		$('#archive_select').change(function(){
			var filename = $(this).val();
			$('#archive_news_get').attr('href', "img/newsletter/"+filename);
		});
	});
});
/****************bg change****************/
if (myUrl['cat'] == "outlets"){
	var tmp = myUrl['page'].split("_");
	if (tmp[0] == "cp")
		$('#page').css('background-image', 'url(img/bg/'+myUrl['cat']+'_cp.jpg)');
	else
		$('#page').css('background-image', 'url(img/bg/'+myUrl['cat']+'_deli.jpg)');
}else
	$('#page').css('background-image', 'url(img/bg/'+myUrl['cat']+'.jpg)');
/*************color/logo theme **********/
if (myUrl['cat'] == "outlets"){
	var wine = myUrl['wine'];
	var m = myUrl['m'].split('_');
	var tmp = myUrl['page'].split("_");
	
	$('#outlets_smenu_'+tmp[1]).css('color', '#FFFFFF');
	if (tmp[0] == "cp"){
		$('#outlets_sub_menu a').addClass('cp_theme');
		$('#'+m[1]).addClass('cp_theme');
		$('#wine_'+wine).addClass('cp_theme');
		$('#header_logo').css('background-image', 'url(img/template/logo_restaurant.png)');
		//$('body').supersleight({shim: 'blank.gif'});
	}else{
		$('#outlets_sub_menu a').addClass('deli_theme');
		$('#header_logo').css('background-image', 'url(img/template/logo_deli.png)');
		$('#menu_outlets a').removeClass('cp_theme');
		$('#menu_outlets a').addClass('deli_theme');
		$('#'+m[1]).addClass('deli_theme');
		$('#wine_'+wine).addClass('deli_theme');
		//$('body').supersleight({shim: 'blank.gif'});
	}
}
/****************************************/
/*			sub menu outlets			*/
	$('#outlets_sub_menu a').click(function(){
		var tmp = myUrl['page'].split("_");
		var id = $(this).attr('href');
		id = id.replace("#", "");
		window.location = "?cat="+myUrl['cat']+"&page="+tmp[0]+"_"+id;
	});
//sub menu other
	$('#contact_sub_menu a').click(function(){
		var tmp = myUrl['page']
		var id = $(this).attr('href');
		id = id.replace("#", "");
		window.location = "?cat="+myUrl['cat']+"&page="+id;
	});
/****************************************/
	$('#header_logo').click(function(){
		window.location = "?page=welcome";
	});

	$('#main_menu a').mouseover(function(){
		var id = $(this).attr('href');
		id = id.replace("#", "");
		
		$('#header_bottom ul').css('visibility', 'hidden');
		$('#smenu_'+id).addClass('smenu_cat_selected');
		$('#smenu_'+id).css('visibility', 'visible');
	});
		$('#header_bottom ul').mouseleave(function(){
			$(this).css('visibility', 'hidden');
		});
	$('#header_bottom a').click(function(){
		var file = $(this).parents('ul').attr('id');
		file = file.replace("smenu_", "");
		var id = $(this).attr('href');
		id = id.replace("#", "");
		window.location = "?cat="+file+"&page="+id;
	});
	/**************news switch*************/
	$('#welcome_news_menu a').click(function(){
		var id = this.id.split("_");
		$('#news_template img').css('z-index', 0);
		$('#template_'+id[1]).css('z-index', 5);
	});
	/**************chefs switch***********************/
	var chefs_start = 0;
	$('#chefs_sub_menu a').click(function(){
		$('#chefs_sub_menu a').removeClass('mywhite');
		
		$('#chefs_sub_menu a').each(function(){
			if (!$(this).hasClass('cp_theme'))
			$(this).addClass('cp_theme');
		});
		$(this).removeClass('cp_theme');
		$(this).addClass('mywhite');
		switch(this.id){
			case 'patrick':
				$('.title').text("PATRICK GOUBIER");
				$('#img_desc').text("Patrick Goubier - Executive Chef");
				
			break;
			case 'gerome':
				$('.title').text("GEROME HAUTH");
				$('#img_desc').text("Gérôme Hauth - Head Chef");
			break;
			case 'xuyen':
				$('.title').text("XUYEN GOUBIER");
				$('#img_desc').text("Xuyen Goubier - Pastry Chef");
			break;
		}
		
		$('#chefs_bio').children('p').each(function(){
			$(this).hide();
		});
		
		$('#chefs_'+this.id).show();
		$('#mychefpic').attr('src', 'img/chefs_slides/'+this.id+".jpg").load(function(){
			$('#img_desc').css({'width': $('#mychefpic').width()+"px"});
		});
	});
	/******************event******************/
	$('.event_title').click(function(){
		animate("set", 0);

		//calcul space with history
		var m_sel = $(this).parents('.event_range_m').attr('id')
		//alert($(this).parents('.event_range_m').attr('id'));
		var m_tot = 0;
		var flag = 0;
		$('.event_range_m').each(function(){
			if (flag == 0)
				m_tot += 0;
			if (this.id == m_sel)
				flag = 1;
		});
		//alert(m_tot);
		if ($(this).next('div').hasClass('on'))
			$(this).next('div').slideUp();
		else{
			$(this).next().slideDown(function(){
				$(this).addClass('on');
				$('.event_content').each(function(){
				if (!$(this).hasClass('on'))
					$(this).slideUp();
				});
			});
			//open article on the top
			var tot = 0;
			var flag = 0;
			var sel = this.id;
			var nb = 0;
			$('.event').each(function(){
				var id = $(this).children().attr('id');
				if (flag <= 0){
					tot += $(this).children().height();
					nb++;
				}
				if (id == sel)
					flag = 1;
			});
			tot = ((tot - 40) + (nb * 10) - m_tot)* - 1;
			animate("set", tot); 
		}
		$('.event_content').removeClass('on');
	});
	
	//menu color
	var myevent = "event_current";
	if (myUrl['archive'] == 1)
		myevent = "event_previous";
	$('#'+myevent).css('color', '#FFFFFF');
		
/*************** scroler up/down***********/
	var timeout = "";
	$("#up").mousedown(function(){
		clearInterval(timeout);
		timeout = setInterval(function(){
			animate("up", 20)
		}, 100);
		//animate("up", 100); 
	}); 
	$("#down").mousedown(function() {
		clearInterval(timeout);
		timeout = setInterval(function(){
			animate("down", 20)
		}, 100);
		//animate("down", 100); 
	});
	$('#up, #down').mouseup(function(){
		clearInterval(timeout);
	});
	/*****************product switch**********************/
	var prodpage = 1;
	$('.products_select').click(function(){

		$('#products_list li').removeClass('products_active');
		$(this).addClass('products_active');
		$('#product_desc p').hide();
		prodpage = $(this).text();
		$('#image_area img').css('z-index', 1);
		$('#image_area img:nth-child('+prodpage+')').css('z-index', 9999);
		$('#image_area_title').text($('#image_area img:nth-child('+prodpage+')').attr('alt'));
		$('#prod'+prodpage).show();
	});
	/*************picture gallery*******/
	$('#galleryDisplay a').lightBox({fixedNavigation:true});
	var nb = $('#galleryDisplay').children().length;
	//spacing between 2 lign
	var spacing = 185;
	//position start
	var pos = 0;
	//nb image by cols
	var nbcol = 4;
	//nb lign images
	var nblign = 2;
	//animation speed
	var gallerySpeed = 500;
	
	var posmax = (nb / nbcol) * spacing;
	
	$('#scrollGallery > img').click(function(){
	//alert(nb + " " + nbcol + " " + spacing + " " + posmax);	
		switch(this.id){
			case 'downGallery' :
				pos -= spacing * nblign;
				if (pos <= -(posmax-spacing))
					pos = 0;
			break;
			case 'upGallery' :
				pos += spacing * nblign;
				if (pos >= spacing)
					pos = -(posmax-((spacing*nblign) - 0));
			break;
			default :
				pos = 0;
			break;
		}
		$('#galleryDisplay').stop().animate({ marginTop: pos+'px'}, gallerySpeed);
	});
/************switch picture******************/
	$(".current").fadeTo(1,1);
	
	$("#slide_switch_img").click(function(){
			var $current = $("#sliderImage div.current");
			
			if ( $current.length == 0 )
				$current = $("#sliderImage div:last");
		
			var $next =  $current.next().length ? $current.next() : $("#sliderImage div:first");
			
			$current.addClass('prev');
			
			$next.css({opacity: 0.0}).addClass("current").animate({opacity: 1.0}, 500, function() {
					$current.css({opacity:0.0});
					$current.removeClass("current prev");			 
				});
			//$("#numImg").html($next.attr("id"));
	});
	
	/*******/
	$(function() {  
        $("#controller").jFlow({  
            slides: "#slides",  
            width: "350px",  
            height: "255px",  
            duration: 600  
        });  
    }); 
	/*********mini gallery*********/
	$(function(){
		var pos = 1;
		var nb = total = $('#image_area').children().length;
		$('#image_area_total').text(total);
		
		$('#image_area img:nth-child(1)').css('z-index', 9999);
		$('#prev').click(function(){
			pos--;
			if (pos < 1)
				pos = total;
			$('#image_area img').css('z-index', 1);
			$('#image_area img:nth-child('+pos+')').css('z-index', 9999);
			var alt = $('#image_area img:nth-child('+pos+')').attr('alt');
			$('#image_area_text').text(alt);
			$('#image_area_nb').text(pos);
		});
		$('#next').click(function(){
			pos++;
			if (pos > total)
				pos = 1;
			$('#image_area img').css('z-index', 1);
			$('#image_area img:nth-child('+pos+')').css('z-index', 9999);
			var alt = $('#image_area img:nth-child('+pos+')').attr('alt');
			$('#image_area_text').text(alt);
			$('#image_area_nb').text(pos);
		});
	});
	/********************email form************/
	/*$('#work_form').submit(function(){
		myajax("actions/work.php", $(this).serialize());
	});*/
	$('#catering_form').submit(function(){
		myajax("actions/catering.php", $(this).serialize());
	});
	$('#reservation_form').submit(function(){
		myajax("actions/reservation.php", $(this).serialize());
	});
	$('#private_form').submit(function(){
		myajax("actions/private.php", $(this).serialize());
	});
	$('#newsletters_form').submit(function(){
		myajax("actions/newsletter.php", $(this).serialize());
	});
	/***********************outlets menus**************/
	//add menus_list
	//to menu
	var m = myUrl['m'].split("_");
	if (m[0] == "cp" || m[0] == "deli"){
		$('.bloc_right').addClass('menus_list');
		$('.bloc_right').addClass('flexcroll');
	}
	//to winelist
	var wine = myUrl['wine'];
	if (wine >= 1 && wine <= 5){
		$('.bloc_right').addClass('menus_list');
		$('.bloc_right').addClass('flexcroll');
	}
	/*********outlet sub menu**********/
	$('.menu_type').click(function(){
		var tmp = this.id;
		
		if ($(this).hasClass('cp_menu')){
			$('.menu_type').removeClass('cp_theme');
			$(this).addClass('cp_theme');
		}else{
			$('.menu_type').removeClass('deli_theme');
			$(this).addClass('deli_theme');
		}
		
		$('.menus_list').children().each(function(){
			$(this).hide();
			$('#menu_'+tmp).show();
		})
	});
	//switch restaurant location
	
	
});

function animate(dir, step){
	var position; 
	position = parseInt($("#sliderText").css("margin-top")); 

	var contentheight; 
	var sliderheight; 
	contentheight = parseInt($("#sliderText").css("height")); 
	sliderheight = parseInt($("#slider").css("height")); 
	//alert("sliderheight"+sliderheight+"contentheight"+contentheight+"position"+position);
	switch (dir){ 
		case "down": 
			position = ((position - step) <= (- contentheight)) ? position:(position - step); 
			break; 
		case "up": 
			position = ((position + step) > 0) ? 0 : (position + step); 
			break; 
		case "set": 
			position = step; 
			break; 
		default: 
		break; 
	}; 
	$("#sliderText").stop().animate({ marginTop: position + "px" }, 100 );
}
function myajax(url, form){
	$.ajax({
		type: "POST",
		url: url,
		data: form,
		async: false,
		success: function(msg){
			//alert( "Data Saved: " +msg );
			alert(msg);
		}
	});
}
function redi(){
	var value = $('#location').val();
	$('.shop_location div').not('.'+value).hide();
	$('.shop_location .'+value).show();
	$('.deli_map').attr('src', 'img/template/map_'+value+'.jpg');
}
