function goToByScroll(id, adjust) {
    $('html, body').delay(20).animate( { 
        scrollTop: $("#"+id).offset().top + adjust
    }, 'fast');
    $("#navigation").animate({"left": "-300px"}, 500);
    $('#backtotop').fadeIn();
}

function transitionOut(delay) {
    $('#curtain').delay(delay).animate({"top": "-1130px"}, 1600, "easeOutElastic", function() {
        $('html').css({"overflow-y": "scroll"})
    });
    $("#navigation").delay(delay).animate({"left": "-300px"}, 800);
    $("#signup_panel").delay(delay).animate({"right": "-436px"}, 1000);
    $("#share_tab").delay(delay).animate({"bottom": "-40px"}, 1000);	
    $('#tasslecontainer').delay(delay).animate({"top": "-250px"}, 500, "easeOutQuad");
    $('#header').delay(delay).animate({"top": "0px"}, 500, "easeOutQuad");
}

function transitionIn(delay) {
    $('#curtain').delay(delay).animate({"top": "-450px"}, 1600, "easeOutQuad");
    $('html').delay(delay).css({"overflow-y": "hidden"});
    $("#navigation").delay(delay).animate({"left": "-536px"}, 500);
    $("#signup_panel").delay(delay).animate({"right": "-500px"}, 500);
    $("#share_tab").delay(delay).animate({"right": "-140px"}, 500);	
    $('#tasslecontainer').delay(delay).animate({"top": "-20px"}, 500, "jswing");
    $('#header').delay(delay).animate({"top": "-300px"}, 700, "easeOutQuad");
}

function transitionInNav(delay, url) {
    $('html').delay(delay).css({"overflow-y": "hidden"});
    $("#navigation").delay(delay).animate({"left": "-500px"}, 500);
    $("#signup_panel").delay(delay).animate({"right": "-500px"}, 500);
    $("#share_tab").delay(delay).animate({"right": "-140px"}, 500);	
    $('#tasslecontainer').delay(delay).animate({"top": "-500px"}, 500, "jswing");
    $('#header').delay(delay).animate({"top": "-300px"}, 700, "easeOutQuad");
    $('#curtain').delay(delay).animate({"top": "-450px"}, 1600, "easeOutQuad", function() {
        // Cheeky hack. REMOVE!
        if(window.location.host == "localhost") {
          window.location.href = "http://localhost/~martin/php/odd" + url;
        } else {
          window.location.href = url;
        }
    });
}

function goTop() {
    $('html, body').delay(20).animate( { 
        scrollTop: 0
    }, 'fast');
    $('#backtotop').fadeOut();
}

function goOdd() {
    transitionInNav(300, "/odd/");
    //transitionInNav(300, "http://127.0.0.1/~jag/wordpress/odd/odd/");
}

function goOdder() {
    transitionInNav(300, "/odder/");
    //transitionInNav(300, "http://127.0.0.1/~jag/wordpress/odd/odder/");
}

function goOddest() {
    transitionInNav(300, "/oddest/");
    //transitionInNav(300, "http://127.0.0.1/~jag/wordpress/odd/oddest/");
}

function showAbout() {
    $("#about_container").fadeIn();
    goToByScroll("about_container", -40);
    $("#navigation").animate({"left": "-300px"}, 500);
}

function hideAbout() {
    $("#about_container").fadeOut();
}

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    window.onscroll = function() {
        var newtop = window.pageYOffset + window.innerHeight - 55;
        $("#share_tab").animate({"top": newtop }, 500);
        $("#signup_panel").animate({"top": newtop - 1000 }, 500);
        $("#navigation").animate({"top": newtop - 1000 }, 500);
    };
}

$(document).ready(function() {
    
    /* Listen to calendar clicks */
    $('table#calendar a').click(function() {
        $('#event_display_detail').jcarousel({
           scroll: 1,
           animation: 'slow',
           wrap: 'circular',
           easing: ''
        });
    });
    
    $(window).scroll(function() {
       if($(window).scrollTop() >= 300) {
           $('#backtotop').fadeIn();
       } else {
           $('#backtotop').fadeOut();
       }
    });
	
	var setZIndex = function() {
		var image_count = $images.length;
		$(".fader img").each(function(index) {
			$(this).css({"z-index": image_count-index});
		});
	}
	
	// Bar Image fader
	var $fader = $(".fader");
	if($fader.length > 0) {
		var $images = $("img", $fader);
		
		$fader.css({
			"position": "relative",
			"overflow": "hidden"
		});
		
		$images.css({
			"position": "absolute",
			"top": "0",
			"left": "0"
		});
		
		setZIndex();
		
		setInterval(function() {
			var $current_image_array = $(".fader img")
			var $first_image = $current_image_array.eq(0);
			//console.log($first_image);
			$first_image.fadeOut(1600, function() {
				$first_image.appendTo($first_image.parent());
				setZIndex();
				$current_image_array.css({"display": "block"});
				
			});
		}, 5000);
	}
	
    /* 
      jQuery Placeholder cross trouser
      Thanks http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
    */
    $("'[placeholder]'").focus(function() {
      var input = $(this);
      if (input.val() == input.attr("'placeholder'")) {
        input.val("''");
        input.removeClass("'placeholder'");
      }
    }).blur(function() {
      var input = $(this);
      if (input.val() == "''" || input.val() == input.attr("'placeholder'")) {
        input.addClass("'placeholder'");
        input.val(input.attr("'placeholder'"));
      }
    }).blur();
    
    $('#event_carousel').jcarousel({
       scroll: 1,
       animation: 'slow',
       auto: 2,
       wrap: 'last',
       easing: ''
    });
    
    $('div#events_toggle').click(function() {
        if($('#upcoming_events').height() == 696) {
            $('#upcoming_events').animate({
                height: 1060
            });
            $('a#events_toggle').css('background-position', '0px 0px');
        } else {
            $('#upcoming_events').animate({
                height: 696
            });
            $('a#events_toggle').css('background-position', '0px -48px');
        }
    });

    if($('#drinks_offers_list > li').size() > 3) {
        $('#drinks_offers_list').jcarousel({
           vertical: true,
           scroll: 1,
           animation: 'slow',
           wrap: 'last',
           auto: 2
        });
    }
    
    if($('#food_offers_list > li').size() > 3) {
        $('#food_offers_list').jcarousel({
           vertical: true,
           scroll: 1,
           animation: 'slow',
           wrap: 'last',
           auto: 2
        });
    }
    
    if($('#event_display_detail_inner > li').size() > 1) {
        $('#event_display_detail').jcarousel({
           scroll: 1,
           animation: 'slow',
           wrap: 'circular',
           easing: ''
        });
    }
 
    $("#menutoggle").toggle(function() {
        if(Math.floor(parseInt($("#navigation").css('left'))) == -300) {
            $("#navigation").animate({"left": "-100px"}, 1500, "easeOutElastic");
        } else {
            $("#navigation").animate({"left": "-300px"}, 1500, "easeOutElastic");
        }
    }, function() {
        if(Math.floor(parseInt($("#navigation").css('left'))) == -100) {
            $("#navigation").animate({"left": "-300px"}, 1500, "easeOutElastic");
        } else {
            $("#navigation").animate({"left": "-100px"}, 1500, "easeOutElastic");
        }
    })
    
    $("#signuptoggle").toggle(function() {
        if(Math.floor(parseInt($("#signup_panel").css('right'))) == -436) {
            $("#signup_panel").animate({"right": "-143px"}, 1500, "easeOutElastic");
        } else {
            $("#signup_panel").animate({"right": "-436px"}, 1500, "easeOutElastic");
        }
    }, function() {
        if(Math.floor(parseInt($("#signup_panel").css('right'))) == -100) {
            $("#signup_panel").animate({"right": "-436px"}, 1500, "easeOutElastic");
        } else {
            $("#signup_panel").animate({"right": "-143px"}, 1500, "easeOutElastic");
        }
    })
    
    $("#tassle").toggle(function() {
        transitionOut(0);
    }, function() {
        transitionIn(0);
    })
    
    if($("body").hasClass("internal")) {
        transitionOut(300);
        $("#tassle").toggle(function() {
            transitionIn(0);
        }, function() {
            transitionOut(0);
        })
    }
    
    $("a.hotspotfour").fancybox({'titleShow': true });
    $("a.hotspotfive").fancybox({'titleShow': true });
    $("a.hotspotsix").fancybox({'titleShow': true });
    $("a.hotspotseven").fancybox({'titleShow': true });

		// Ajax Calendar
		$("#calendar a").live("click", function(e) {
			e.preventDefault();
			$("#calendar a").removeClass('active');
			
			$("#event_explorer").load($(this).attr("href") + " #calendar_ajax", function() {
				// Remove wrapper div
				$("#calendar_ajax > div").unwrap();
				// Only set carousel if > 1 child li 
				if($('#event_display_detail_inner > li').size() > 1) {
				    $('#event_display_detail').jcarousel({
                       scroll: 1,
                       animation: 'slow',
                       wrap: 'circular',
                       easing: ''
                    });
				}
			});
			$(this).addClass('active');
			if(history && history.pushState) {
				history.pushState(null, document.title, this.href);
			}
		});
});


