/* 
 * Custom Tickets.ie JS and jQuery Calls
 * jQuery Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

       
	

     $(document).ready(function(){
	 

		 /////////////////// Attach Datepicker to From and To fields ////////////////// 
		 
		 $("#from_daterange").datepick({dateFormat: 'dd/mm/yy'});

		 $("#to_daterange").datepick({dateFormat: 'dd/mm/yy'});
           
        
		 /////////////////// Blur-Focus Search Routine ///////////////////////////////// 
		 
		 $("#main-search-field, #main-search-field-extended").css("color", inactive_color);

		  var default_values = new Array();

		  $("#main-search-field, #main-search-field-extended").focus(function()
		  {
			if (!default_values[this.id]) 
			{
			  default_values[this.id] = this.value;
			}

			if (this.value == default_values[this.id])
			{
			  this.value = '';
			  this.style.color = active_color;
			}

			$(this).blur(function()
			{
			  if (this.value == '') 
			  {
				this.style.color = inactive_color;
				this.value = default_values[this.id];
			  }

			});


		  });

				 /////////////////// Tabbed Area Functionality ///////////////////////////////// 
		
		$("#tabs ul li a").click(function()
		{
			
			// switch all tabs off
			$(".active").removeClass("active");
			$("#competitions-tab").css("background-position", "100% 0");
			$("#news-tab").css("background-position", "100% 0");
			$("#events-tab").css("background-position", "100% 0");

			// switch this tab on
			$(this).addClass("active");
			$(this).css("background-position", "0 0");

			// hide all elements with the class 'tabbed-content'
			$(".tabbed-content").hide();

			// get 'title' attribute and find element with that id and show
			var content_show = $(this).attr("title");
			$("#"+content_show).show();

		});


		$("#competition-links ul li a").mouseover(function()
		{

			// switch off all images and links
			$("#competition-images img.active-image").removeClass("active-image");
			$("#competition-links ul li a.active-link").removeClass("active-link");
			
			// get this links rel tag
			var thisRel = $(this).attr("rel");

			// based on the rel tag located the details for the image about to appear
			var imgAlt = $('#'+thisRel).attr("alt"); 
			var imgURL = $('#'+thisRel).attr("src");

			// set the next image to show with a class of active-image
			$('#'+thisRel).addClass("active-image");

			// set this link to active
			$(this).addClass("active-link");

		});

		$("#competition-links h1 a").mouseover(function()
		{

			$("#competition-links ul li a.active-link").removeClass("active-link");

		});



		$("#event-tab-links ul li a").mouseover(function()
		{

			// switch off all images and links
			$("#event-tab-images img.active-image").removeClass("active-image");
			$("#event-tab-links ul li a.active-link").removeClass("active-link");
			
			// get this links rel tag
			var thisRel = $(this).attr("rel");

			// based on the rel tag located the details for the image about to appear
			var imgAlt = $('#'+thisRel).attr("alt"); 
			var imgURL = $('#'+thisRel).attr("src");

			// set the next image to show with a class of active-image
			$('#'+thisRel).addClass("active-image");

			// set this link to active
			$(this).addClass("active-link");


		});


		$("#event-tab-links h1 a").mouseover(function()
		{

			$("#event-tab-links ul li a.active-link").removeClass("active-link");

		});



		 ///////////////////////////////////////////////////////////////////////////////


		// Upslider Functionality
		$('.single-upslider').hover(function(){
			$(".upslider-cover-details", this).stop().animate({top:'45px'},{queue:false,duration:250});
		}, function() {
			$(".upslider-cover-details", this).stop().animate({top:'95px'},{queue:false,duration:250});
		});

		 ///////////////////////////////////////////////////////////////////////////////



		/////////////////// Attache tablesorter to the event listings table ///////////////////////////////// 
		 
		// id #event-listings-table
		if ( $("#event-listings-table").length > 0 )
		{

			// add parser through the tablesorter addParser method 
			$.tablesorter.addParser({ 
				// set a unique id 
				id: 'ticket-date', 
				is: function(s) { 
					// return false so this parser is not auto detected 
					return false; 
				}, 
				format: function(s) { 
					// format your data for normalization 
					var dateSplit = s.split(' ');

                    var dSplit = dateSplit[1].split('/');
					var day = dSplit[0].replace('01','1').replace('02','2').replace('03','3').replace('04','4').replace('05','5').replace('06','6').replace('07','7').replace('08','8').replace('09','9');
					var month = dSplit[1].replace('01','Jan').replace('02','Feb').replace('03','Mar').replace('04','Apr').replace('05','May').replace('06','Jun').replace('07','Jul').replace('08','Aug').replace('09','Sep').replace('10','Oct').replace('11','Nov').replace('12','Dec'); 
					var year = dSplit[2];
					
					var dateValue = Date.parse(month+' '+day+', '+year);

					return dateValue;
				}, 
				// set type, either numeric or text 
				type: 'numeric' 
			});



			$("#event-listings-table").tablesorter(
			{ 
					
					widgets: ['zebra'],

					headers:
					{ 
						// the second column (we start counting zero) 
						2:{ 
							// apply special date parser - ticket-date
							sorter: 'ticket-date' 
						},
						// the third column (we start counting zero) 
						3:{ 
							// disable it by setting the property sorter to false 
							sorter: false 
						} 
					}
			}).tablesorterPager({container: $("#event-listings-pagination")});
		
		}
		 

		 ///////////////////////// Table Row Hover ///////////////////////////////////
		
		$("#event-listings-table tr").hover(
			function()
			{
				$(this).addClass("highlight");
			},
			function()
			{
				$(this).removeClass("highlight");
			}
		)




		 /////////////////// Side Navigation Date Pickerh ///////////////////////////////// 

			switch(parseInt(jQuery("#search-daterange").val())) {
			 case 1:
			  // show date picker
			  $("#date-range").show();
			  break; 

			  default:
			   // If anything else is selected then hide the date picker
			   $("#date-range").hide();
			}

		 jQuery("#search-daterange").change(function(){
			  
			switch(parseInt(jQuery("#search-daterange").val())) {
			 case 1:
			  // show date picker
			  $("#date-range").show();
			  break; 

			  default:
			   // If anything else is selected then hide the date picker
			   $("#date-range").hide();
			}
			
			

		   });


		 ///////////////////////////////////////////////////////////////////////////////

		
			// BlockUI - jquery extension, documentation here: http://malsup.com/jquery/block/#page
			// can be set to turn off based on feedback from the server handled by AJAX
			// This implementation is on a timer purely for demonstration purposes
			
			$('#splash-demo').click(function() { 
				$.blockUI({ 
				
				// div to show in the overlay
				message: $('#splash-please-wait'), 
				
				css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#EB3E19', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: .9, 
					color: '#FFF' 
				} }); 
		 
				setTimeout($.unblockUI, 10000); 
			}); 

		 ///////////////////////////////////////////////////////////////////////////////
			
			
			// Countdown Timer that goes to ticket release page
//			$(function () {
//
//				$('#countdown-timer').countdown({
//			
//				until: '+6m +00s',
//				expiryUrl: 'http://www.tickets.ie', 
//				format: 'MS',
//				description: 'hardcoded'
//				});
//		});




		/////////////////// Competitions slider//////////////////////////////

			// id #slider exists
			if ( $("#slider").length > 0 )
			{

						var $panels = $('#slider .scrollContainer > div');
						var $container = $('#slider .scrollContainer');

						// if false, we'll float all the panels left and fix the width 
						// of the container
						var horizontal = true;

						// float the panels left if we're going horizontal
						if (horizontal) {
						  $panels.css({
							'float' : 'left',
							'position' : 'relative' // IE fix to ensure overflow is hidden
						  });
						  
						  // calculate a new width for the container (so it holds all panels)
						  $container.css('width', $panels[0].offsetWidth * $panels.length);
						}

						// collect the scroll object, at the same time apply the hidden overflow
						// to remove the default scrollbars that will appear
						var $scroll = $('#slider .scroll').css('overflow', 'hidden');

						// offset is used to move to *exactly* the right place, since I'm using
						// padding on my example, I need to subtract the amount of padding to
						// the offset.  Try removing this to get a good idea of the effect
						var offset = parseInt((horizontal ? 
						  $container.css('paddingTop') : 
						  $container.css('paddingLeft')) 
						  || 0) * -1;


						var scrollOptions = {
						  target: $scroll, // the element that has the overflow
						  
						  // can be a selector which will be relative to the target
						  items: $panels,
						  
						  navigation: '.navigation a',
						  
						  // selectors are NOT relative to document, i.e. make sure they're unique
						  prev: 'a.left, a.imgLeft', 
						  next: 'a.right, a.imgRight',
						  
						  // allow the scroll effect to run both directions
						  axis: 'xy',

						  cycle:true, // Cycle endlessly ( constant velocity, true is the default )
						  // force:true,  // force to start srolling
						  // interval:5000,	// interval inbetween each automatic scroll	  
						  offset: offset,
						  
						  // duration of the sliding effect
						  duration: 500,
						  
						  // easing - can be used with the easing plugin: 
						  // http://gsgd.co.uk/sandbox/jquery/easing/
						  easing: 'swing'
						};

						// apply serialScroll to the slider - we chose this plugin because it 
						// supports// the indexed next and previous scroll along with hooking 
						// in to our navigation.
						jQuery('#slider').serialScroll(scrollOptions);

						// now apply localScroll to hook any other arbitrary links to trigger 
						// the effect
						jQuery.localScroll(scrollOptions);

						// finally, if the URL has a hash, move the slider in to position, 
						// setting the duration to 1 because I don't want it to scroll in the
						// very first page load.  We don't always need this, but it ensures
						// the positioning is absolutely spot on when the pages loads.
						scrollOptions.duration = 1;
						jQuery.localScroll.hash(scrollOptions);
			
			
			} //end if slider exists
				

			/////////// Make Competition Boxes Clickable / highlight on rollover ///////////////////////////////

			$(".competition-cell").click(function()
			{
				window.location=$(this).find("a").attr("href");return false;
			});


			$(".competition-cell").hover(
			   function()
			   {
				
				// roll in
				$(this).css("background-image","url(content/images/slider-competition-rollover.gif)")
				$(this).css("cursor","pointer"); 

			   },
			   function()
			   {
				// roll out
				$(this).css("background-image","url(content/images/slider-competition-cell-bg.gif)");
				
			   }
			  )



		 /////////////////// Support/FAQ AJAX Results Pagination ///////////////////////

		 
		 $(".faq-page-num").click(function(){

			// update the current page num on click
			var currentPageNum = $(this).attr('rel');
			$("#ajax-results-pagination h2 span#currentpagenum").html(currentPageNum);

			// page number persistence
			$(".faq-page-num").removeClass("current-faq-pg");
			$(this).addClass("current-faq-pg");
			
			

			// hide all page dives
			$("#support-faq-page-1, #support-faq-page-2, #support-faq-page-3, #support-faq-page-4, #support-faq-page-5, #support-faq-page-6, #support-faq-page-7").hide();
			

			// show the div relevant to the page number clicked
			$("#support-faq-page-"+currentPageNum).show();		



		 });

		 /////////////////// Dynamic Country/County Select Fields ////////////////// 

             

              switch($("#country-select").val()) {
                case 'IE' :
                    $("#other-city").hide()
                    $("#other-city-textfield").val("");
                    $("#irish-county").show()
                    break;    
                   default:
                     $("#irish-county").hide()
                     $("#other-city").show()
                  };

              
              $("#country-select").change(function(){
                  switch(($("#country-select").val())) {
                   case 'IE':
                    // show Irish county dropdown
                    $("#other-city").hide()
                    $("#other-city-textfield").val("");
                    $("#irish-county").show()
                    break;    
                   default:
                     $("#irish-county").hide()
                     $("#other-city").show()
                  }

 

              });


		 
		 


		 /////////////////// Dynamic FAQ results rollover ////////////////// 

/*
			// bind events???
			$(".ajax-query-result-preview").hover(
			function()
			{
		    
				// roll in
				$(this).find("h4 a").css("color","#FF0").css("text-decoration","underline");
				$(this).css("cursor","pointer"); 

			},
			function()
			{
				// roll out
				$(this).find("h4 a").css("color","#FFF").css("text-decoration","none");
				$(this).css("border","0px")
			});	 

			$(".ajax-query-result-preview").click(function()
			{
				window.location=$(this).find("a").attr("href");return false;
			});
*/

			$(".ajax-query-result-preview").live("mouseover", function()
			{
		    
				// roll in
				$(this).find("h4 a").css("color","#FB762A").css("text-decoration","underline");
				$(this).css("cursor","pointer"); 

			});
			
			$(".ajax-query-result-preview").live("mouseout", function()
			{
				// roll out
				$(this).find("h4 a").css("color","#FFF").css("text-decoration","none");
				$(this).css("border","0px")
			});	
	
			$(".ajax-query-result-preview").live("click", function()
			{
				window.location=$(this).find("a").attr("href");return false;
			});
			
		 ///////////////////////////////////////////////////////////////////////////////


			// Social Bookmarks
//			$('#social-bookmarks img').hover(function(){
//				
//				
//				// get alt tag value for this icon
//				var addToText = $(this).attr("alt");
//				
//				// update <p> with addToText
//				$("#add-to-site").text(addToText);


//				
//			}, function() {
//				
//				
//				
//			});


//			// use plugin to show alternate social bookmarking popup
//			$('#bookmark_popup_text').bookmark({
//			
//				popup: true,
//				popupText: '<img src="../../content/images/other-bookmarks.gif" width="26" height="26" alt="Click to view full list" class="bookmark_popup_text"/>'
//			});
//		 
		  
		  
		 ///////////////////////////////////////////////////////////////////////////////


		 // show/hide artist/venue panels

		// hide more button on load
		$(".more").hide();

		$('.less').click(function(){
			$("#venue-general-info").slideUp("slow");
			$("#artist-general-info").slideUp("slow");
			$(this).hide();
			$('.more').show();
		});

		$('.more').click(function(){
			$("#venue-general-info").slideDown("slow");
			$("#artist-general-info").slideDown("slow");
			$(this).hide();
			$('.less').show();
		});
		
		
		 ///////////////////////////////////////////////////////////////////////////////

		 //create a mytickets account on the customer details page of the booking process
		
			$("#mytickets-create-ac").click(function()
			{   
				
				$("#create-mytickets").slideToggle(500);
			});


		 // forgot password form reveal
		
			$(".forgot-password").click(function()
			{   	
				$("#forgotten-password").slideToggle(500);
			});
				
			
			
		
		///////////////////////////////////////////////////////////////////////////////

		// terms and conditions checkbox on customer details page
	

		$("#customer-details-form").submit(function()
		{   
			
			if($('#tandc-checkbox').attr('checked'))
			{
				$('cust-details').submit();
				return true;
			}
			else
			{
				$('#terms-agree').show();
				return false;
				
			}
		});



		///////////////////////////////////////////////////////////////////////////////

		// customer support tabbed area
	

		$("#contact-form").click(function()
		{   
			
			// remove active-support-tab class
			$("#support-tabs a").removeClass("active-support-tab");
			$(this).addClass("active-support-tab");
			$("#tabbed-contact-details").hide();
			$("#tabbed-contact-form").show();

		});


		$("#email-phone").click(function()
		{   
			
			$("#support-tabs a").removeClass("active-support-tab");
			$(this).addClass("active-support-tab");
			$("#tabbed-contact-form").hide();
			$("#tabbed-contact-details").show();

		});


		
		
		
		///////////////////////////////////////////////////////////////////////////////


		// checkbox mytickets alert tree functionality
		 $("ul.alert-tree").checkTree();

		 // show/hide categories


		$(".show-categories").click(function(){

			$(".alert-categories").slideToggle(500);

		});

		 
		 ///////////////////////////////////////////////////////////////////////////////



		// Media asset download rollover
		$('.media-asset').hover(function(){
			$(this).css("background-image","url(content/images/media-asset-on.gif)");
			$(this).css("cursor","pointer");
		}, function() {
			$(this).css("background-image","none");
		});

		
		$(".media-asset").click(function()
		{
			window.location=$(this).find("a").attr("href");return false;
		});




		 



             
     }); // end jQuery calls


	


/* STANDARD JavaScript Starts HERE */
	 

	// focus/blur routine for main search
	
	var active_color = '#000'; // Colour of user provided text
	var inactive_color = '#555'; // Colour of default text


	
	function checksearch(form)
	{
		var searchstring = document.mainsearch.s.value;

		if (searchstring=="" || searchstring=="Enter Artist, Event, Team or Venue...")
		{
			alert("You have not entered anything to search!")
			
		}
		else
		{
		    form.submit();
		}
}

//Adds a bookmark for the given title and URL
function bookmarksite(title, url)
{
    if (window.sidebar)
    {
        // firefox
        window.sidebar.addPanel(title, url, "");
    }
    else if (window.opera && window.print)
    {
        // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all)
    {
        // ie
        window.external.AddFavorite(url, title);
    }
}
