var myScroll;
function loaded() {
	if($("#content>ul>li").length > 1){
		myScroll = new iScroll('content', {
			vScroll : false,
			snap: true,
			momentum: true,
			hScrollbar: false,
			vScrollbar: false,
			onScrollStart: function(){
				if(myScroll.x > 0){
					$("#previous").css({opacity : 1});
				}else{
					$("#previous").css({opacity : .3});
				}
				if(myScroll.x < $("#viewer").width()){
					$("#next").css({opacity : 1});
				}else{
					$("#next").css({opacity : .3});
				}
			}
		});
		if(myScroll.currPageX == 0){
			$("#previous").css({opacity : .3});
		}


	}else{
		$("#nav").remove();
	}

}
function modalContent(data, id){
	if ($("#dialog").length < 1) {
		$('body').append("<div id='dialog'><div></div></div>");
	}
	if (!isEmpty(data)) {
		title_text = "";
		$("#dialog").hide();
		$("#dialog").dialog("destroy");
					$("#dialog").load(data, function(){
						title_text = $("#dialog h3").text();
						$("#dialog h3").hide();
						$("#dialog").attr("title", title_text);

						$("#dialog").show();
						$("#dialog").dialog({
							modal : true,
							resizable : false,
							position : 'center',
							width : 590,
							height : 560,
							closeOnEscape : true,
							close : function(event, ui) {
								modalContent();
							}
						});

						$(".ui-dialog-titlebar").hide();
						$(".ui-dialog").removeClass("ui-widget-content");
						$(".ui-dialog").removeClass("ui-corner-all");
						$(".cform textarea").attr('rows', '6');
						$(".linklove").remove();
						var hereColor = $("#content").attr('class');
						$("#dialog>div").addClass(hereColor);


						if($('#dialog>.invitation').length > 0){
							alert('test');
							$('.cf-ol').append('<div id="facebook"><a href="http://www.facebook.com/pages/Galerie-Italienne/" target="_blank"><img src="/wp-content/themes/galerieitalienne/images/facebook.jpg" /></a></div>')
						}


						$(".cform input:submit").bind('click', function(){
							$(".cform").submit(function(event){
								event.preventDefault();
								var dataForm = $(".cform").serialize();
								console.log(dataForm);
								$.ajax({
									url : $(".cform").attr('action'),
									data : dataForm,
									type: 'POST',
									success : function(){
										modalContent();
									}
								});
								//alert('submit');
							});

						});
						$("#dialog").append("<p class='close_part'>Close <span id='close'><em>&nbsp;</em></span></p>");
						$("#close").bind('click', function(){
							modalContent();
						});
					});



	} else {
		$("#dialog").dialog( "destroy" );
		$("#dialog").remove();
	}
}

function splashscreen(show)
{
	if (show) {
		$("#slideshow").dialog({
			modal : true,
			resizable : false,
			position : 'center',
			width : 735,
			height : 550,
			closeOnEscape : true,
			close : function(event, ui) {
				splashscreen();
			}
		});
		$(".ui-dialog-titlebar").hide();
		$(".ui-dialog").removeClass("ui-widget-content");
		$(".ui-dialog").removeClass("ui-corner-all");
		$("#slideshow").append("<p class='close_part'>Close <span id='close'><em>&nbsp;</em></span></p>");
		$("#close").bind('click', function(){
			splashscreen();
		});
		$(".ui-widget-overlay").bind('click', function(){
			splashscreen();
		});
		$('#slideshow').dialog( "option", "minHeight", 550 );
	}else {
		$("#slideshow").dialog( "destroy" );
		$("#slideshow").remove();
	}
}

$(document).ready(function(){
		if($("#list_artist").length > 0){
			$("#list_artist").jScrollPane();
		}
		$("#viewer").css({width : (($("#content>ul>li").length) * $(".panel").width())+"px"});
		$("#screen").css({width : (($("#screen> .slide_large").length) * 735)+"px"});
		if($(".scroll-pane").length>0){
			$(".scroll-pane").jScrollPane({
				autoReinitialise: true/*,
				animateScroll: true,
				animateEase : "easeout"*/
			});
		}

		$(".ilike").click(function(event){
			event.preventDefault();
			iLike($(this).attr('href'));
		});

		$('#bio_content').mouseover(function(){
			$('#bio_content>.jspVerticalBar').fadeIn('2000');
		}).mouseout(function(){
			$('#bio_content>.jspVerticalBar').fadeOut('3000');
		});

		function iLike(urlPage){
			var destination = "facebook";
			var url = urlPage;
			var pub_key = "84bdf89a-9d9c-4c04-8142-1cb449308f1b";
			var access_key = "f30adc414efe28a57d7216d5ee46d2b2";
			window.open("http://rest.sharethis.com/share/sharer.php?destination="+destination+"&url="+url+"&pub_key="+pub_key+"&access_key="+access_key);
			return;
		}

		$("#next").click(function(){
			myScroll.scrollToPage('next', 0);
			if(myScroll.currPageX <= $("#content>ul li").length-1){
				$("#next").css({opacity : .3});
				$("#previous").css({opacity : 1});
			}else{
				$("#next").css({opacity : 1});
			}
			return false;
		});
		$("#previous").click(function(){
			myScroll.scrollToPage('prev', 0);
			if(myScroll.currPageX <= 0){
				$("#next").css({opacity : 1});
			}else{
				$("#previous").css({opacity : 1});
				$("#next").css({opacity : .3});
			}
			return false;
		});

		$("a[rel^='lightbox']").bind('click', function(){
			//alert($(this).attr('title'));
		});
});
document.addEventListener('DOMContentLoaded', loaded, false);
function isEmpty(obj) {
    if (typeof obj == 'undefined' || obj === null || obj === '') return true;
    if (typeof obj == 'number' && isNaN(obj)) return true;
    if (obj instanceof Date && isNaN(Number(obj))) return true;
    return false;
}

