$(document).ready(function(){
	/*
	$(document).bind("contextmenu",function(e){
        return false;
    });
	*/ 
	if ($.browser.msie  && parseInt($.browser.version) == 6 )
	{
		alert("Best viewed with IE7 or above, Firefox 3.6 or above, Safari 4 or above, Chrome 14.0 or above");
	}
	
	$("#t2").bind("contextmenu",function(e){
        return false;
    });
	
	
	$("#t2").bind("contextmenu",function(e){
        return false;
    });
	
	$('a[rel=popup]').click(function(){
		var link1 = $(this).attr("href");
		window.open (link1, "mywindow","resizable=1,width=725,height=600, scrollbars=yes");
		return false;
	});

	if ( $.browser.msie )
	{
		$('img.corner_left').attr('src', '/sitedesign/images/home/submenu-corner-left-1.png');
		$('img.middle').attr('src', '/sitedesign/images/home/submenu-bg-bot-1.png');
		$('img.corner_right').attr('src', '/sitedesign/images/home/submenu-corner-right-1.png');
	} 
	
	/*
	var originalFontSize = $('body').css('font-size');
	$("a.fontsize-m").click(function(){
		if($(".fontsize-m a.active").length <= 0) {
			removeTabActive("span.fontselector");
			$(this).addClass("active");
			$('body').css('font-size', originalFontSize);
			currentFontSize = 'm';
		}
	});
	$("a.fontsize-l").click(function(){
 		if($(".fontsize-l a.active").length <= 0) {
 			removeTabActive("span.fontselector");
			$(this).addClass("active");
			var currentFontSize = $('body').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*2; //1.2;
			$('body').css('font-size', newFontSize);
		}
		return false;
	});
	$("a.fontsize-s").click(function(){
		if($(".fontsize-s a.active").length <= 0) {
			removeTabActive("span.fontselector");
			$(this).addClass("active");
			var currentFontSize = originalFontSize; //$('body').css('font-size');			
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*0.8; //0.8;
			$('body').css('font-size', newFontSize);
		}
		return false;
	});
	*/

	/*
	$("a.progimagelist").fancybox({
		'titlePosition'  : 'over'
	});
	
	$("a[rel=group_image]").fancybox({
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'titlePosition' : 'over',
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + title + '</span>';
		}
	});
	
	*/
	$("a[rel=group_image]").fancybox({
		'titlePosition' : 'inside' 
	});
	

 	if( $('a[rel^="videopopup"]').length > 0) {
		$("a[rel^=videopopup]").click(function() {
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: true,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
				}
			});
	
			return false;
		});
		
 		/*
		$('a[rel^="videopopup"]').each(function(index, value) {
			var flv = $(this).attr("href");
 			var flashvars = {"flv": flv};			
			var attributes = {"FlashVars": "flv="+flv+"&showfullscreen=1", "allowFullScreen": true};
			
			$(this).attr("href", "/media/flash/player.swf");
			$("#"+$(this).attr("id")).fancybox({
				'padding'			: '0',
				'autoScale'			: true,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'autoDimensions'	: false, 
				'scrolling'			: 'no',
				'cyclic'			: false,
				'showNavArrows'		: false,
				'swf'				: attributes,
				'type'				: "swf"
			});
		});
		*/
	}

 	/* share panel
	if( $('a[rel^="popup"]').length > 0) {
		$('a[rel^="popup"]').each(function(index, value) {
 			var Id = $(this).attr("id");
 			$('#'+Id).fancybox({
				'width'				: '56',
				'height'			: '70',
				'padding'			: '0',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'autoDimensions'	: false, 
				'scrolling'			: 'no',
				'cyclic'			: false,
				'showNavArrows'		: false,
				'type'				: 'iframe'
			});
			delete Id;
		});
	}
	*/
	
	if( $("#btn-delivery-submit").length > 0 ) {
		var lang = (/en|tc|sc/.exec(location.pathname.substring(1, 3).toLowerCase()) || ['en'])[0];
		$("#btn-delivery-submit").click( function() {
			if( $("input.DeliveryMethod:checked").length == 0 ) {
				if(lang == 'tc')
					alert('請選擇門票送遞方式。\n');
				else if(lang == 'sc')
					alert('请选择门票送递方式。\n');
				else
					alert('Please select the delivery method.\n');
				return false;
			} else {
				return true;
			}
		});
	}

	initTab(".contentTab", "#details .box-m");
	initTab(".mediatab", "div.mediaContent");
	
	if( $(".contentTab").length > 0 ) {
		$(".contentTab").click( function() {
			if( $(this).attr("id") == "tab2") {
				$("a.have-up-arrow-link").show();	
			} else {
				$("a.have-up-arrow-link").hide();	
			}
		});
	}
	
	if( $("a.have-up-arrow-link").length > 0 ) {
		$("a.have-up-arrow-link").click( function() {
			$("#about_this_programme div.content").each( function( index, ele ) {
				if( $(this).hasClass("inactive") ) {
					$(this).removeClass("inactive");
					$(this).addClass("active");

					if( $("#about_this_programme div.content").length == (index+1) ) {
						$("a.have-up-arrow-link").hide();
					}
					return false;
				}
			});
		});
	}

	initFlashPlayer("videolist");
	initFlashPlayer("audiolist");
	
	if($("div.scrollable1").length > 0) { 
		$("div.scrollable1 div.col").css("position","relative");
		$("div.scrollable1 div.col").css("width","auto");
	}
	if($("div.scrollable2").length > 0) { 
		$("div.scrollable2 div.col").css("position","relative");
		$("div.scrollable2 div.col").css("width","190px");
	}
	if($("div.scrollable3").length > 0) { 
		$("div.scrollable3 div.col").css("position","relative");
		$("div.scrollable3 div.col").css("width","190px");
	}

	//if($("div.scrollable1").length > 0) { 
		$("div.scrollable1").smoothDivScroll({ 
			autoScrollDirection: "backandforth", 
			autoScrollStep: 1, 
			autoScrollInterval: 15,	
		 
			startAtElementId: "startAtMe"
		});
	//}
	//if($("div.scrollable2").length > 0) { 
		$("div.scrollable2").smoothDivScroll({ 
			autoScrollDirection: "backandforth", 
			autoScrollStep: 1, 
			autoScrollInterval: 15,	
			startAtElementId: "startAtMe1"
		});
	//}
	//if($("div.scrollable3").length > 0) { 
		$("div.scrollable3").smoothDivScroll({ 
			autoScrollDirection: "backandforth", 
			autoScrollStep: 1, 
			autoScrollInterval: 15,	
			startAtElementId: "startAtMe2"
		});
	//}
	//$("div.scrollable3").smoothDivScroll("restoreOriginalElements");

	if($('#newsletter_email').length > 0) {
		var defaulttext = $('#newsletter_email').attr("rel");
		$('#newsletter_email').default_text(defaulttext);
		delete defaulttext;
	}

	if($('form.newsletter_subscription').length > 0 && $('input#newsletter_subscribe').length > 0) {
		$('input#newsletter_subscribe').click(function() {
			var flag = $("form.newsletter_subscription").validate({
				rules: {
					email: {
							required: true,
							email: true
					}
				},
				messages: {
					email: {
						required: "",
						email: ""
					}
				}
			}).element( "#newsletter_email" );
			if(!flag) 
				alert('Please enter a valid email.');
			return flag;
		});		
	}
	
	function showViewBySelector() {
		if( $("#viewtype").val() == "name" ) {
			$("#viewcat").hide();	
			$("#viewdate").hide();
 		} else if( $("#viewtype").val() == "category" ) {
			$("#viewcat").show();	
			$("#viewdate").hide();				
		} else if ( $("#viewtype").val() == "date" ) {
			$("#viewcat").hide();	
			$("#viewdate").show();				
		}	
	}
	
	if( $("#viewtype").length > 0 ) {
		showViewBySelector();

		$("#viewtype").change( function() {
			showViewBySelector();
			if( $(this).val() == "date" ) {
				$("form#artlist").submit();
			}
			if( $(this).val() == "name" ) {
				$("form#artlist").submit();
			}
			
		});

		$("#viewcat").change( function() {
			$("form#artlist").submit();
		});

		$("#viewdate").change( function() {
			$("form#artlist").submit();
		});
	}

	if($('div.programitem').length > 0) {
		$('div.programitem').hover( function() {
			$(this).addClass("programactive");
			$(this).children(".program-buttons").addClass("program-buttons-active");
		}, 
			function() {
				$(this).removeClass("programactive");
				$(this).children(".program-buttons").removeClass("program-buttons-active");
		});
	}

	if( $("a.choose-book").length > 0 ) {
		$("a.choose-book").click( function() {
			var rel = $(this).attr("rel");
			$('div.performance-popup[rel="'+rel+'"]').show();
			delete( rel );
		});

		$("div.performance-popup a.close").click( function() {
			$(this).parent().parent().parent().hide();
		});
	}

	if( $("img.btn-search").length > 0 ) {
		$("img.btn-search").click( function() { 
			if( $("#venue").val() != '' ) 
				location.href = $("#venueform").attr("action") + "venue/"+$("#venue").val();
		});
	}

	if( $("a.seat-map").length > 0 ) {
		$("a.seat-map").click( function() {
			var rel = $(this).attr("rel");
			$('div.seat-map-window[rel="'+rel+'"]').show();
		});

		$("div.seat-map-window a.close").click( function() {
			$(this).parent().parent().parent().hide();
		});
	}

	if( $("a.venue_popup").length > 0 ) {
		$('a.venue_popup').fancybox({
			'width'				: 700,
			'height'			: 600,
			'padding'			: '0',
			'autoScale'			: false,
			'autoDimensions'	: false, 
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'scrolling'			: 'no',
			'cyclic'			: false,
			'showNavArrows'		: false,
			'type'				: 'iframe'
		});
	}


	function initFlashPlayer(obj){
		var objSelector = "." + obj;
		var itemSelector = "#" + obj;
		
		if( $(objSelector).length > 0) {
			var params = {}; 
			
			$.each(objSelector, function(i, v) {
				if($(itemSelector+i).length > 0) {
					 
					var flv = $(itemSelector+i).attr("rel");
					 
					var flashvars = {"flv": flv};			
					var attributes = {"FlashVars": "flv="+flv+"&showfullscreen=1", "allowFullScreen": true};
					if (obj == "audiolist")
						swfobject.embedSWF("/media/flash/player.swf", obj+i, "185", "35", "9.0.0", flashvars, params, attributes);
					else
						swfobject.embedSWF("/media/flash/player.swf", obj+i, "185", "129", "9.0.0", flashvars, params, attributes);

					delete flashvars, flv, attributes;			
				}
			});
			delete params;
		}	
	}
 
	function showTabContent(Id) {
		if($("#"+Id).length > 0){
			$("#"+Id).addClass("active");
		}
	}

	function removeTabActive(objSelector) {
		$.each($(objSelector), function(index, value) { 
			$(this).parent().removeClass("active");			
		});
	}

	function initTab(tabObj, contentObj) {
		if( $(tabObj).length > 0) {
			$(tabObj).click(function() {
				removeTabActive(tabObj);
				$(this).parent().addClass("active");

				var Id = $(this).attr("rel");
				
				if($(contentObj).length > 0) 
					$(contentObj).find(".active").removeClass("active");
							
				$("#"+Id).addClass("active");
				
				delete Id;
			});
		}
	}
	

});

 
