// catch if console doesn't exist (IE)
function log(value) {
	if(typeof(console) != "undefined") {
		console.log(value);
	}
}

var tPanelTimeout;
var inProgress = false;
$(document).ready(function() {
    // Calendar tabs
    $(".tab-feb").click(function() {
		$(this).addClass("selected");
		$(".table-mar").hide();
		$(".table-feb").show();
		$(".link-feb").addClass("disabled");
		$(".link-mar").removeClass("disabled2");
		$(".tab-mar").removeClass("selected");
		return false;
    });

    $(".tab-mar").click(function() {
		$(this).addClass("selected");
		$(".table-feb").hide();
		$(".table-mar").show();
		$(".link-feb").removeClass("disabled");
		$(".link-mar").addClass("disabled2");
		$(".tab-feb").removeClass("selected");
		return false;
    });

    // Calendar links
    $(".link-feb").click(function() {
		$(this).addClass("disabled");
		$(".table-mar").hide();
		$(".table-feb").show();
		$(".tab-feb").addClass("selected");
		$(".link-mar").removeClass("disabled2");
		$(".tab-mar").removeClass("selected");
		return false;
    });

    $(".link-mar").click(function() {
		$(this).addClass("disabled2");
		$(".table-feb").hide();
		$(".table-mar").show();
		$(".tab-mar").addClass("selected");
		$(".link-feb").removeClass("disabled");
		$(".tab-feb").removeClass("selected");
		return false;
    });

    // Calendar past events
    $("#past-events-header .btn_hide").live("click",function(e) {
		if (e.which != 3) {
			if($(this).text("Show").hasClass("btn_show")) {
			    $(this).text("Hide").removeClass("btn_show");;
			    $("#past-events").slideDown("slow",function() {
			    	$(".review").show();
				});
			}
			else {
			    $(".review").hide();
			    $("#past-events").slideUp("slow");
			    $(this).text("Show").addClass("btn_show");
			}
		}
		return false;
	});

	check_past_events();

    // Sidebar Calendar
    $(".cal_table td").click(function() {
		if ( !$(this).hasClass("empty") ) {
		    $(".cal_table td").removeClass("selected");
		    $(this).addClass("selected");
		    $(".bigtabs a").removeClass("selected");

		    //$(".events-loading").show();
		    $(".events-overlay").show();

		    if ( !$(this).parent().hasClass("days") ) {
				var day = $(this).text();
				var month = "mar";

				if ($(".tabs .tab.selected").hasClass("tab-feb")) {
				    var month = "feb";
				}

				if($("#events-content").length > 0) {
				    $.get("/london/ajax/", { calendar:'day', day: day, month: month },
				    function(data) {
						//$(".events-loading").hide();
						$("#events-content").empty().append(data);
						$(".events-overlay").hide();
						check_past_events();
				    });

					return false;
				}
				else {
					return true;
				}
		    }
		}
		return false;
    });

    // Gallery dots
    $(".quickgallery .nav .navdot").click(function() {
		var oldImage = $(".quickgallery .nav .selected").attr("rel");
		var newImage = $(this).attr("rel");

		if ( !$(this).hasClass("selected") ) {
		    if ( !$(".quickgallery #imageGallery img").is(':animated') ) {
				$(".quickgallery .nav .navdot").removeClass("selected");
				$(this).addClass("selected");
				fadeImage(oldImage, newImage);
		    }
		}

		return false;
    });

    // Gallery left/right
    $(".quickgallery .nav #btn_navarrow_left").click(function() {
		var oldImage = $(this).attr("rel");

		if (oldImage != 1) {
		    var newImage = parseInt(oldImage) - 1;

		    if ( !$(".quickgallery #imageGallery img").is(':animated') ) {
			fadeImage(oldImage, newImage);
		    }
		}

		return false;
    });

    var size = $(".quickgallery .nav .navdot").size();

    $(".quickgallery .nav #btn_navarrow_right").click(function() {
		var oldImage = $(this).attr("rel");

		if (oldImage != size) {
		    var newImage = parseInt(oldImage) + 1;

		    if ( !$(".quickgallery #imageGallery img").is(':animated') ) {
			fadeImage(oldImage, newImage);
		    }
		}

		return false;
    });

    // Calendar tabs
    $(".bigtabs a").click(function() {
		if ( !$(this).hasClass("selected") ) {

			if($(this).hasClass('comingsoon')) {
				return false;
			}

		    $(".bigtabs a").removeClass("selected");
		    $(this).addClass("selected");

		    //$(".events-loading").show();
		    $(".events-overlay").show();

		    var week = $(this).attr("rel");

		    $.get("/london/ajax/", { calendar:week },

			function(data) {
			    //$(".events-loading").hide();
			    $("#events-content").empty().append(data);
			    $(".events-overlay").hide();
				check_past_events();
			});
		}
		return false;
    });

	$(".comingsoon").hover(function() {
		$(this).find('.info').hide();
		$(this).find('.date').hide();
		$(this).find('.soon').show();
	}, function() {
		$(this).find('.info').show();
		$(this).find('.date').show();
		$(this).find('.soon').hide();
	});

	$("#btn_export,.panel_export").mouseenter(function() {
		$(".panel_export").show();
		clearTimeout(tPanelTimeout);
		return false;
    });

	$("#btn_export,.panel_export").mouseleave(function() {
		clearTimeout(tPanelTimeout);
		tPanelTimeout=setTimeout("$(\".panel_export\").hide();",800);
		return false;
    });

    // Home page featured nav hover
    $("#home-nav li").hover(function() {
		var color = $(this).css("background-color");
		$(this).css("background-color","#e6e6e6");
		$(this).css("color","#000000");
		$(this).find("a").css("color","#000000");
    },
    function () {
		if ( $(this).hasClass("selected") ) {
		    $(this).css("background-color","#e6e6e6");
		    $(this).css("color","#000000");
		    $(this).find("a").css("color","#000000");
		} else {
		    $(this).css("background-color","#000000");
		    $(this).css("color","#ffffff");
		    $(this).find("a").css("color","#ffffff");
		}
    });

    // IE6 for homepage submit button
    if ( $.browser.msie && $.browser.version == 6) {
		$("#home-updates .submit").hover(function () {
	    	$(this).css("backgroundPosition","0 -26px");
		}, function() {
	    	$(this).css("backgroundPosition","0 0");
		});
    }

    // Home page featured nav click
    $("#home-nav li").click(function() {
		if(inProgress || $(this).hasClass('comingsoon') || $(this).hasClass('selected')) {
			return false;
		}

		inProgress = true;
		$("#home-nav li.selected").css("background-color","#000000");
		$("#home-nav li.selected").css("color","#ffffff");
		$("#home-nav li").removeClass("selected");
		$(this).addClass("selected");

		var rel = $(this).attr("rel");
		$("#home-nav .arrow").removeClass().addClass('arrow');
		if(rel != "i-calendar") {
			$("#home-nav .arrow").addClass(rel);
		}

		$('#feature-overlay-1').addClass('feature-overlay-loading');
		$(".feature-overlay").css({'opacity':1});
		$("#feature-overlay1").css({"left": "475px"});
		$("#feature-overlay2").css({"left": "475px"});
		$("#feature-overlay3").css({"left": "475px"});
		$("#feature-overlay4").css({"left": "237px"});
		$("#feature-overlay5").css({"left": "239px"});

		var wk = rel.match(/\d{1,}\.{0,}\d{0,}/);

		if (wk == parseInt(wk)) {
		    if ( $("#home-weekview").hasClass("home-weekview") ) {
				animateFeatured(wk);
		    } else {
				showFeatured(wk);
		    }
		}

		if (rel != "i-calendar")   return false;
	});

    $("#home-nav li.h-featured").click(function() {
		$("#home-weekview").removeClass("home-weekview");
		$("#home-weekview").hide();
		$("#home-slideshow").show();
		$('#home-featured-artists a.prev').click();
		$('#home-featured-artists a.prev').click();
		$('#home-featured-artists a.prev').click();
		inProgress = false;

		return false;
   	});

    $(".home-feature-box").live("click",function(e) {
		if (e.which != 3) {
			if(!$(this).hasClass('disabled')) {
				window.location = $(this).find("a").attr("href");
			}
		}
    });

    $("#home-weekview").live("mouseover",function() {
		if(inProgress) {
			return false;
		}
		$('#feature-overlay-1').removeClass('feature-overlay-loading');
		$(".feature-overlay").css({'left':0});
		$(".feature-overlay:not(.show)").stop().fadeTo('fast', .7);
	});
    $("#home-weekview").live("mouseout",function() {
		if(inProgress) {
			return false;
		}

		$('#feature-overlay-1').addClass('feature-overlay-loading');
		$(".feature-overlay").stop().fadeTo('fast', 0);
	});

    $(".home-feature-box").live("mouseover",function() {
		if(inProgress) {
			return false;
		}

		if(!$(this).hasClass('disabled')) {
			$(this).find(".feature-overlay").stop().addClass('show').fadeTo('fast', 0);
		}
	});
    $(".home-feature-box").live("mouseout",function() {
		if(inProgress) {
			return false;
		}

		if(!$(this).hasClass('disabled')) {
			$(this).find(".feature-overlay").stop().removeClass('show').fadeTo('fast', .7);
		}
	});

    // calendar events hover
    $("#london_calendar .eventbox").live("mouseover", function() {
		$(this).addClass('eventbox_hover');
		$(this).find('.hoversmallthumb').addClass('hoversmallthumb_hover');
    });

    $("#london_calendar .eventbox").live("mouseout", function() {
		$(this).removeClass('eventbox_hover');
		$(this).find('.hoversmallthumb').removeClass('hoversmallthumb_hover');
    });

    $('#london_calendar .eventbox').live("click", function(e) {
		if (e.which != 3) {
			window.location = $(this).find('a:first').attr('href');
		}
    });

	if($('.cal_area .selected').length > 0) {
		$('.cal_area .cal_table').hide();
		if($('.cal_area .selected').parents('.cal_table').hasClass('table-feb')) {
			$('.tabs .tab-feb').click();
		}
		else {
			$('.tabs .tab-mar').click();
		}
	}

    // artists individual artist hover + gallery hover
    $('#london_artists .hoversmallthumb_item, #london_gallery .hoversmallthumb_item').hover(function() {
	    $(this).find('.hoversmallthumb').addClass('hoversmallthumb_hover');
	    $(this).find('.hoversmallthumb_details').addClass('hoversmallthumb_details_hover');
    }, function() {
	    $(this).find('.hoversmallthumb').removeClass('hoversmallthumb_hover');
	    $(this).find('.hoversmallthumb_details').removeClass('hoversmallthumb_details_hover');
    });

 	// hoverthumb event detail
    $('.hoverthumb_item, #gallery .hoverthumb_item').hover(function() {
	    $(this).find('.hoverthumb').addClass('hoverthumb_hover');
	    $(this).find('.hoverthumb_details').addClass('hoverthumb_details_hover');
    }, function() {
	    $(this).find('.hoverthumb').removeClass('hoverthumb_hover');
	    $(this).find('.hoverthumb_details').removeClass('hoverthumb_details_hover');
    });

	// Home event
	$('.home-event-container').hover(function() {
	    $(this).find('.home-event-link').addClass('home-event-link-hover');
    }, function() {
	    $(this).find('.home-event-link').removeClass('home-event-link-hover');
    });

	// lecture videos individual hover
    $('#lecture-videos .hoversmallerthumb_item').hover(function() {
	    $(this).find('.hoversmallerthumb').addClass('hoversmallerthumb_hover');
	    $(this).find('.hoversmallerthumb_details').addClass('hoversmallerthumb_details_hover');
    }, function() {
	    $(this).find('.hoversmallerthumb').removeClass('hoversmallerthumb_hover');
	    $(this).find('.hoversmallerthumb_details').removeClass('hoversmallerthumb_details_hover');
    });

	$('#lecture-videos .hovertallthumb_item').hover(function() {
	    $(this).find('.hovertallthumb').addClass('hovertallthumb_hover');
	    $(this).find('.hovertallthumb_details').addClass('hovertallthumb_details_hover');
    }, function() {
	    $(this).find('.hovertallthumb').removeClass('hovertallthumb_hover');
	    $(this).find('.hovertallthumb_details').removeClass('hovertallthumb_details_hover');
    });


	/* Lecture Videos Alterations */
	$('.tx-rbmavideoplayer-pi1 .videoInfoBox').wrapAll('<div id="videoInfoBoxWrap"></div>');

	$('.tx-rbmavideoplayer-pi1 .videoInfoBox').each(function(index) {
		$(this).attr('id', 'videoInfoBox_'+(index+1));
	});

	$('.tx-rbmavideoplayer-pi1 .videoInfoBox:last').hide();

	$('.tx-rbmavideoplayer-pi1 .videoInfoBox p').each(function(index) {
		$(this).attr('id', 'videoInfoBox_p_'+(index+1));
	});

	$('.tx-rbmavideoplayer-pi1 .videoColumn299 p').each(function(index) {
		$(this).attr('id', 'videoColumn299_p_'+(index+1));
	});

	$('.tx-rbmavideoplayer-pi1 .videoColumn299 h4').hide();
	$('.tx-rbmavideoplayer-pi1 .videoColumn299 #videoColumn299_p_3').hide();
	$('.tx-rbmavideoplayer-pi1 .videoColumn299 #videoColumn299_p_4').hide();

	$('#videoInfoBoxWrap').append('<div class="videoInfoBox" id="videoInfoBox_99"><h3 class="videoInfoBoxHeadline">Embed Video</h3><p class="videoInfoBoxInfo" id="videoInfoBox_p_99"></p></div>');
	$('.tx-rbmavideoplayer-pi1 .videoColumn299 #emb_low').appendTo('#videoInfoBox_p_99');
	$('.tx-rbmavideoplayer-pi1 .videoColumn299 #emb_high').appendTo('#videoInfoBox_p_99');
	$('#videoInfoBox_p_99 h4').show();

	$('.videoColumn456 img').each(function(index) {
		$(this).attr('src', '/' + $(this).attr('src'));
	});

	if($('h3.greyCapsHeadline').length > 1) {
		var artists = '';
		$('h3.greyCapsHeadline').each(function(index) {
			artists += $(this).text();
			if(index < $('h3.greyCapsHeadline').length - 1) {
				artists += ", ";
			}
		});

		$('h3.greyCapsHeadline').empty();
		$('h3.greyCapsHeadline:first').html(artists);
	}

	$('.videoColumn299 #videoColumn299_p_1').css('top', (160 + $('h3.greyCapsHeadline:first').height()) + 'px');
	$('.videoColumn456').css('paddingTop', (40 + $('h3.greyCapsHeadline:first').height()) + 'px');

	$('#home-updates .submit').click(function() {
		$.post('/london/ajax/?signup=true',{
		 	email: $('#home-updates .email').val()
		},
		 function(data){
			$('#home-updates p').html(data);
			$('#home-updates .email').val("");
		});
		return false;
	});

});

$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

jQuery.easing.easeInOutQuart = function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
    return -c/2 * ((t-=2)*t*t*t - 2) + b;
}

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
}

jQuery.easing.easeOutBack = function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}

jQuery.easing.easeInOutCirc = function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
    return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
}

jQuery.easing.easeInOutBack = function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}

jQuery.easing.easeInSine = function (x, t, b, c, d) {
    return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
}

jQuery.easing.easeOutCirc = function (x, t, b, c, d) {
    return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
}


jQuery(function( $ ){
	$('#home-featured-slideshow').serialScroll({
		items:'li',
		prev:'#home-featured-artists a.prev',
		next:'#home-featured-artists a.next',
		offset:0,
		duration:800,
		force:true,
		stop:true,
		lock:true,
		step:1,
		cycle:false,
		easing:'easeInOutQuart',
		onBefore:function(e, elem, $pane, $items, pos) {
		    nextImage = parseInt(pos) + 1;
		    prevImage = parseInt(pos) - 1;
		    if (pos == 1) {
				$("#home-featured-artists .prev").removeClass("disabled");
		    }
		    if (pos == 0) {
				$("#home-featured-artists .prev").addClass("disabled");
		    }
		    if (pos == 2) {
				$("#home-featured-artists .next").addClass("disabled");
		    }
		    if (pos < 2) {
				$("#home-featured-artists .next").removeClass("disabled");
		    }
		    $("#home-featured-title li").hide();
		    $("#home-featured-artists li").hide();
		    $("#home-featured-title .title-"+nextImage).show();
		    $("#home-featured-artists .artists-"+nextImage).show();
		}
	});

	$('#recent-slideshow').serialScroll({
		items:'li',
		prev:'#home-recent a.prev',
		next:'#home-recent a.next',
		offset:0,
		duration:800,
		force:true,
		stop:true,
		lock:true,
		step:4,
		exclude:0,
		cycle:false,
		easing:'easeInOutQuart',
		onBefore:function(e, elem, $pane, $items, pos) {
		    if (pos == 0) {
				$("#home-recent .next").removeClass("disabled");
				$("#home-recent .prev").addClass("disabled");
		    }
		    if (pos == 4) {
				$("#home-recent .prev").removeClass("disabled");
				$("#home-recent .next").addClass("disabled");
		    }
		    if (pos == 7) {
				return false;
		    }
		    // Can unbind then attach it again?
		    //$('#recent-slideshow').unbind('.serialScroll');
		}
	});
});

function checkSize() {
    result = $('#home-featured-slideshow ul li').size();
    return result;
}

function showFeatured(wk) {
    //$("#home-overlay").show();

    $("#featured-slider").animate({"left": "0"}, 200, 'linear', function() {
		$("#home-weekview").addClass("home-weekview");
		var path = "/london/ajax/?homeweek="+wk;
	    $.get(path, function(data) {
			$("#home-slideshow").hide();
			$("#home-weekview").empty();
			$("#home-overlay").fadeOut();
			$("#home-weekview").show().append(data);
			setTimeout(function() {
			    $("#featured-slider").animate({"left": "951px"}, 400, 'linear', function() {
					$("#featured-slider").css("left", "951px");
					$(".feature-overlay").css({'opacity':0});
					inProgress = false;
			    });
			}, 100);
	    });
    });
}

function animateFeatured(wk) {
    var path = "/london/ajax/?homeweek="+wk;

    $(".feature-overlay").addClass("hidden");

    $("#feature-overlay1").animate({"left": "0"}, 200, 'linear');
    $("#feature-overlay2").animate({"left": "0"}, 200, 'linear');
    $("#feature-overlay3").animate({"left": "0"}, 200, 'linear');
    $("#feature-overlay4").animate({"left": "0"}, 200, 'linear');
    $("#feature-overlay5").animate({"left": "0"}, 200, 'linear', function() {
	    $.get(path, function(data){
			loadFeatured(data);
	    });
    });
}

function loadFeatured(data) {
    $("#home-weekview").empty().append(data).hide();
    $(".feature-overlay").addClass("hidden");

    $("#feature-overlay1").css("left", "0");
    $("#feature-overlay2").css("left", "0");
    $("#feature-overlay3").css("left", "0");
    $("#feature-overlay4").css("left", "0");
    $("#feature-overlay5").css("left", "0");

    $("#home-weekview").show();
    removeFeaturedOverlay();
}

function removeFeaturedOverlay() {
    setTimeout(function() {
		$("#feature-overlay1").animate({"left": "475px"}, 300, 'linear', function() {
		    $(this).removeClass("hidden");
			$(".feature-overlay").css({'opacity':0});
			inProgress = false;
		});
    }, 100);

	setTimeout(function() {
		$("#feature-overlay2").animate({"left": "475px"}, 300, 'linear', function() {
		    $(this).removeClass("hidden");
		});
    }, 100);

	setTimeout(function() {
		$("#feature-overlay3").animate({"left": "475px"}, 300, 'linear', function() {
		    $(this).removeClass("hidden");
		});
    }, 100);

    setTimeout(function() {
		$("#feature-overlay4").animate({"left": "237px"}, 300, 'linear', function() {
		    $(this).removeClass("hidden");
		});
    }, 100);

    setTimeout(function() {
		$("#feature-overlay5").animate({"left": "239px"}, 300, 'linear', function() {
		    $(this).removeClass("hidden");
		});
    }, 100);
}

function randomNo() {
    var randVal = Math.floor(500+(Math.random()*(1000-500)));
    return (randVal);
}

function fadeImage(oldImage, newImage) {
    $(".quickgallery .nav #btn_navarrow_right").attr("rel",newImage);
    $(".quickgallery .nav #btn_navarrow_left").attr("rel",newImage);

    $(".quickgallery .nav .navdot").removeClass("selected");
    $(".quickgallery .nav .navdot[rel="+newImage+"]").addClass("selected");

    $(".quickgallery #imageGallery img[rel="+newImage+"]").addClass("behind").removeClass("hide").css("opacity","1");

    $(".quickgallery #imageGallery img[rel="+oldImage+"]").stop().animate({"opacity": "0"}, 500, 'linear', function() {
		$(this).removeClass("show").addClass("hide");
    });
    $(".quickgallery #imageGallery img[rel="+newImage+"]").stop().animate({"opacity": "1"}, 500, 'linear', function() {
		$(this).removeClass("behind").addClass("show");
    });
}

function check_past_events() {
	if($('#upcoming-events-header').length == 0) {
		$('#past-events').show();
		$('#past-events-header a').text('Hide').removeClass("btn_show");
	}
}

var rbmaPopup = '';
function popup_movie(url, w, h) {
	if (rbmaPopup && !rbmaPopup.closed)	{
		rbmaPopup.focus();
	}
	else {
		rbmaPopup=window.open(url,'name','height=' + h + ' ,width=' + w + ',titlebar=0,statusbar=0,status=0,resizable=0,locationbar=0,location=0,menubar=0,personalbar=0,scrollbars=0,toolbar=0,addressbar=0');
		if (!rbmaPopup.opener) rbmaPopup.opener = self;
	}
	if (window.focus) {
		rbmaPopup.focus();
	}
}

// Facebook Connect functions
function onConnected(user_id) {
	$('#header_right_top').css('visibility', 'visible');
}
function onNotConnected() {
	$('#header_right_top').css('visibility', 'hidden');
}