// DEFAULT VAR
var pageList = {"#Home":{"id":"sectionHome","marginLeft":"0"},
				"#Gallery":{"id":"sectionGallery","marginLeft":"1420"},
				"#Private":{"id":"sectionPrivate","marginLeft":"2850"},
				"#Contact":{"id":"sectionContact","marginLeft":"4260"}};
var autoSelect = window.location.hash;
var selectOpacity = 1;


function gotoPage(idPage, opacity){
	currentPage = '';
	if(pageList[idPage] == null) {
		currentPage = pageList['#Home'];
	}else{
		currentPage = pageList[idPage];
	}
	
	$('#caruselWrapper').animate({
		scrollLeft : currentPage['marginLeft']
	},  900, 'easeInOutExpo', function(){
		$("#" + currentPage['id']). animate({opacity: opacity}, 200);
		//hack fix all
		$(".bigCarusel li").css("opacity", 1);
	});                
	return false;
}

function initLayouts(){
	// adjust width fake :D // stupid thing
	var tempWidth = (($("body").width() - 900) / 2) - 25;
	$("#sectionfake").width(tempWidth);
}

function servicesSlide(moveTo){
	$('.contentTextSlide').stop().animate({
		scrollTop : moveTo
	},  350, 'easeInOutExpo', function(){
	});
}

Shadowbox.init({
	skipSetup: true
});

jQuery(document).ready(function(){
	$('.nav').click(function () {
		var moveTo = 0;
		var stepSize = 785;
		var direction = $(this).attr('class').replace("nav ", "");
		var currentPage = $(this).parent().parent();
		var currentPos = currentPage.find('.galleryCarusel').scrollLeft();
		var limitSize = ((currentPage.find(".imgSlideshow").size() / 3) * stepSize) - stepSize;
		
		if(direction == 'next'){
			if(currentPos < limitSize){
				moveTo = currentPos + stepSize;
			}else{
				moveTo = 0;
			}
		} else if(direction == 'prev'){
			if(currentPos > 0){
				moveTo = currentPos - stepSize;
			}else{
				moveTo = 0;
			}
		}
		
		currentPage.find('.galleryCarusel').stop().animate({
			scrollLeft : moveTo
		},  350, 'easeInOutExpo', function(){
			
		});
		
	});

});



function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

var currentShow = '';
var currentClasa = '';
var caruselFilter = function(clasa){
	$(".imgSlideshow").css('display', 'none');
	currentClasa = clasa;
	currentShow += "," + clasa;
	$(currentShow).css('display', 'block');
	
	$(".galleryMenu").animate({
		opacity : 0,
		bottom: '150px'
	}, {duration: 300, easing: 'easeOutBack'});
}



$(function(){
	// PRELOAD				   
	$(".bigCarusel li").css("opacity", 1);
	initLayouts();
	gotoPage(autoSelect, 1);
	var clickOpacity = 1;
	
	$(".galleryMenu").css('opacity', 0);
	
	
	// EVENTS 
	
	$(window).resize(function() {
	initLayouts();
	});
	
	$(".categoriesBtn").click(function(){
		if($(".galleryMenu").css('opacity') == 0){
			$(".galleryMenu").animate({
				opacity : 1,
				bottom: '160px'
			}, {duration: 400, easing: 'easeOutBack'});
		}else{
			$(".galleryMenu").animate({
				opacity : 0,
				bottom: '150px'
			}, {duration: 400, easing: 'easeOutBack'});
		}
		return;
	});
	
	
	$('a.galleryShadowbox').click(function(){
		lien = 'gallery.php?album_dir='+$(this).attr('id');
		Shadowbox.open({
			content: lien,
			player: "iframe",
			scrolling:"no",
			height:425,
			width:620
		});
	});
	
	$('a.flvShadowbox').click(function(){
		$('#jquery_jplayer').jPlayer("stop");
		Shadowbox.open({
			content: $(this).attr('url'),
			player: "flv",
			scrolling:"no",
			height:360,
			width:480
		});
	});
	
	$('a.privateShadowbox').click(function(){
		Shadowbox.open({
			content: 'private.php',
			player: "iframe",
			scrolling:"no",
			options:   {handleOversize: 'none', modal: true},
			height:452,
			width:920
		});
	});
	
	$('.galleryMenu-row').click(function(){
		if($(this).find(".checkbox").hasClass('checkboxSelected')){
			$(this).find(".checkbox").removeClass('checkboxSelected');
			$(this).find(".checkbox").css('background-position', '0px -19px');
			var temcurrentShow = str_replace("," + currentClasa, "", currentShow);
			$(".imgSlideshow").css('display', 'block');
			//$(temcurrentShow).css('display', 'block');
		}
		$(this).find(".checkbox").addClass('checkboxSelected');
	});	
	
	$('.private_connect a').click(function(){
		$.ajax({
			type: "POST",
			url: 'index.php',
			data: {form_id : 'private_form', id: $('#private_pwd').val()},
			dataType: "json",
			success: function( data ) {
				if(!data.error){
					//$('.private').html('Vous êtes connecté');
					
					location.reload();
				}else{
					$('.private_error').html(data.error);
				}
			},
			error:function (xhr, ajaxOptions, thrownError){
                  alert(thrownError);
            }  
		});
	});
	
	function customer_connect(){
		
	}
	
	$('#btnHome').click(function(){
		gotoPage('#Home');
	});
	$('#btnGallery').click(function(){
		gotoPage('#Gallery');
	});
	$('#btnPrivate').click(function(){
		gotoPage('#Private');
	});
	$('#btnContact').click(function(){
		gotoPage('#Contact');
	});

	// HOME - SLIDER
	$('.slider').cycle({fx: 'fade', timeout:6000, speedIn:  3500, speedOut: 3500});
	
	// CONTACT
	$("#contact_form").validationEngine();
	$('#btnSend').click(function(){
		if($("#contact_form").validationEngine('validate')) {
			$.ajax({
				type: "POST",
				url: 'index.php',
				data: { form_id : 'contact_form', 
						email: $('#contact_email').val(), 
						name: $('#contact_name').val(),
						phone: $('#contact_phone').val(), 
						message:$('#contact_message').val()},
				dataType: "json",
				success: function( data ) {
					if(data.error == ''){
						alert(data.msg);
					}else{
						alert(data.error);
					}
				},
				error:function (xhr, ajaxOptions, thrownError){
					  alert(xhr+' ----> '+thrownError);
				}  
			});
		}
	});
	// MUSIC PLAYER
	$('#jquery_jplayer').jPlayer({
	 swfPath: 'jplayer',
	 solution: 'html, flash',
	 supplied: 'mp3',
	 preload: 'metadata',
	 volume: 0.8,
	 muted: false,
	 autoreplay:true,
	 backgroundColor: '#000000',
	 cssSelectorAncestor: '#jp_control',
	 cssSelector: {
	  play: '.sound-play',
	  pause: '.sound-pause'
	 },
	 ready: function(){
		$(this).jPlayer("setMedia", {
        mp3: "music/amclassical_mozart_adagio.mp3" // Defines the mp3 url
      });
	  $(this).jPlayer("play");
	 },
	 errorAlerts: false,
	 warningAlerts: false
	});
	
	//ZOOMBOX
	$('a.zoombox').zoombox({
		autoplay:true
	});
	$('#weeding_book').click(function(){
		$('#jquery_jplayer').jPlayer("stop");
	});
});
