
/* This function will chop a string to the specified number of characters and add an elipsis (…) if it's longer. If shorter it returns the string unchanged. */ 

function chop(aString, chars) { if (aString.length > chars) { var newString = aString.slice(0, chars); return newString + '&hellip;' } else return aString }


$(document).ready(function () {

    $('.categoryList.sports h3:first').css('margin-top', '0');
    /* Go through the Top Ten List on the page. Cut the event name to fit and the notes. 

    $('.toptenList ol').find('a').each(function(){
    var longString = $(this).html();
    var shortString = chop(longString, 21);
    $(this).html(shortString);
    });
    $('.tooltipcontents').find('p').each(function(){
    var longString = $(this).html();
    var shortString = chop(longString, 29);
    $(this).html(shortString);
    });*/

    /* Swap button image on mouseover. This one does images inside <a> tags... */

    $('a.customButton').each(function (index) {
        var orig = $('img', $(this)).attr("src");
        $($('a.customButton').get(index)).bind('mouseover',
		function (event) {
		    if (orig.indexOf(".gif") != -1) {
		        var over = orig.replace(/.gif$/ig, "-over.gif");
		    } else {
		        var over = orig.replace(/.jpg$/ig, "-over.jpg");
		    }
		    $('img', this).attr({ 'src': over });
		});

        $($('a.customButton').get(index)).bind('mouseout',
		function (event) {
		    $('img', this).attr({ 'src': orig });
		});
    });

    /* ...and this one does image form buttons. */

    $('input.customButton').each(function (index) {
        var orig = $(this).attr("src");
        $($('input.customButton').get(index)).bind('mouseover',
		function (event) {
		    if (orig.indexOf(".gif") != -1) {
		        var over = orig.replace(/.gif$/ig, "-over.gif");
		    } else {
		        var over = orig.replace(/.jpg$/ig, "-over.jpg");
		    }
		    $(this).attr({ 'src': over });
		});
        $($('input.customButton').get(index)).bind('mouseout',
		function (event) {
		    $(this).attr({ 'src': orig });
		});
    });


    /* Remove search field background if there's text */

    var searchBkgdBar = $('#searchbar .searcharea .textfield').css('background-image');
    $('.searcharea .textfield').val('');
    $('.searcharea .textfield').focus(
		function () {
		    $(this).css('background-image', 'none');
		}
	).blur(
		function () {
		    var currentValue = $(this).val();
		    if (currentValue == '') {
		        $(this).css('background-image', searchBkgdBar);
		    }
		}
	)

    var searchBkgdByName = $('#searchbyname .searcharea .textfield').css('background-image');
    $('.searcharea .textfield').val('');
    $('.searcharea .textfield').focus(
		function () {
		    $(this).css('background-image', 'none');
		}
	).blur(
		function () {
		    var currentValue = $(this).val();
		    if (currentValue == '') {
		        $(this).css('background-image', searchBkgdByName);
		    }
		}
	)


    /* Create the scroller on the top bar */

    /*
    $('.scroller div a').each(function(i){
    var thing = $(this).html();
    var newThing = chop(thing, 85);//$('body').append(newThing);
    $(this).html(newThing);
    });
    */

    $('.scroller').cycle({
        fx: 'scrollHorz',
        //	    speed:  'fast', 
        timeout: 8000,
        next: '#next',
        prev: '#prev'
    });

    /* Create the homepage slideshow */
    function onAfter() {
        var imageTwo = $(this).children('.image2');
        var imageThree = $(this).children('.image3');
        imageTwo.fadeIn(function () {
            imageThree.fadeIn();
        });
    }
    if (document.getElementById('slideshow')) {
        $('#slideshow').load('/slideShowContents.aspx #slides > div.slide', function () {
            $("#slideshow").cycle({
                fx: 'fade',
                speed: 1500,
                timeout: 5000,
                pause: 1,
                startingSlide: 0,
                pager: '#slideshownav',
                pagerAnchorBuilder: function (idx, slide) {
                    return '<li><a href="#" style="background-image:url(/GoldCoastImages/btn_slideshownav' + (idx + 1) + '.gif);">&nbsp;</a></li>';
                },
                before: function () { $('.image2').hide(); $('.image3').hide(); },
                after: onAfter
            });
        });
    }


    /* create and launch the tooltips for the Top Ten Lists  */
    /* CONFIG */
    xOffset = 6;
    yOffset = 20;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result		
    /* END CONFIG */
    $("a.tooltip").hover(function (e) {
        this.t = $(this).next('div').html();
        $("body").append("<div id='tooltip'>" + this.t + "</div>");
        $("#tooltip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.css("z-index", 10)
			.show();
    },
	function () {
	    $("#tooltip").remove();
	});
    $("a.tooltip").mousemove(function (e) {
        $("#tooltip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });



    /* create and launch the tooltips for the ticket list Notes  */
    /* CONFIG */
    xOffset = 6;
    yOffset = 20;
    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result		
    /* END CONFIG */
    $("a.notesTip").live('mouseover', function (e) {
        this.t = $(this).next('span').text();
        $("body").append("<div id='notesTip'>" + this.t + "</div>");
        $("#notesTip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.css("z-index", 10)
			.show();
    });
    $("a.notesTip").live('mouseout', function () {
        $("#notesTip").remove();
    });
    $("a.notesTip").live('mousemove', function (e) {
        $("#notesTip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });



    /* Make sure links open in new window */

    $('#page a').attr({ 'target': '_blank' });

    /* Create the pagination for artist text */
    var addMoreLink = false;
    var descHeight = $('#page').outerHeight();
    if (descHeight > 250) {
        var numberOfPages = Math.floor(descHeight / 210) + 1; //Figure out how many pages
        if (numberOfPages > 10) { numberOfPages = 10; addMoreLink = true };     //limit to 10 pages
        var artistText = $('#page').html(); 			 	//Grab the artist ext into a variable
        $('.detailsholder').empty(); 				    //Empty the container so we can fill it with pages
        for (i = 0; i <= numberOfPages - 1; i++) {					//Each page has the entire text content, but is shifted up and
            var shiftUp = 0 - (210 * i); 					//cut off at the bottom to make a page.
            $('.detailsholder').append('<div id="page' + (i + 1) + '" class="page" style="height:225px;"><div style="margin-top:' + shiftUp + 'px"' + artistText + '</div>');
        }
        //Add the nav bar
        $('.detail').append('<div class="nav"><a href="#" id="prevText" style="padding-right:5px;">&lt;Prev</a><span id="pager"></span><a href="#" id="nextText" style="padding-left:5px;">Next&gt;</a></div>');
        //Turn it into a slideshow
        $('.detailsholder').cycle({
            fx: 'fade',
            speed: 'fast',
            timeout: 0,
            next: '#nextText',
            prev: '#prevText',
            nowrap: 1,
            pager: '#pager'
        });
        //add a link to the original profile on last.fm
        var wikiName = $('input#ctl00_ctl00_MainContent_hdConcertName').val();
        wikiName = wikiName.replace(/ /, '+');
        if (addMoreLink) {
            /* $('#nextText').remove(); */
            var moreLink = '<a href="http://www.last.fm/music/' + wikiName + '/+wiki" title="View the full bio of ' + wikiName + ' on Last.fm. Link opens in a new window." target="_blank">More</a>';
            $('#pager').append(moreLink);
        }
    }


    if (document.getElementById('linklistnav')) {
        $('#linklistnav a.ad').css({ 'text-decoration': 'none', 'font-weight': 'bold' })

        $('#linklistnav a').click(function () {
            var whichOne = $(this).attr('class');
            $('#linklist div').hide();
            $('#linklist div.' + whichOne).show();

            $('#linklistnav a').css({ 'text-decoration': 'underline', 'font-weight': 'normal' })
            $(this).css({ 'text-decoration': 'none', 'font-weight': 'bold' })
        });
    }


    if (document.getElementById('acc1')) {
        $('#acc1').accordion({ autoHeight: false, icons: { 'header': 'accordion_arrowright', 'headerSelected': 'accordion_arrowdown'} });
    }
    if (document.getElementById('acc2')) {
        $('#acc2').accordion({ autoHeight: false, collapsible: true, icons: { 'header': 'accordion_arrowright', 'headerSelected': 'accordion_arrowdown'} });
    }
    var insertBar = '<div style="position:absolute;top:-100px;left:-500px;"><img src="/GoldCoastImages/ajax-loader-bar.gif" alt="Processing...please wait."/></div>';
    $('body').append(insertBar);
	
	adjustPaddingOnTicketUnavailableImages();
});


function adjustPaddingOnTicketUnavailableImages() {
  $('.tickets-unavailable').each(function() {
    $(this).parents('td').css({'padding': '0px'});
  });
}

/* STUFF FOR THE CHECKOUT PAGE ONLY */

function checkDomesticShip() {
	$('#acc1').accordion( 'activate' , 1 )
}
	
function launchWindow() {  
   var id = $('#dialog');  
   var maskHeight = $(document).height();  
    var maskWidth = $(window).width();  
    $('#mask').css({'width':maskWidth,'height':maskHeight});  
    $('#mask').show();      
    $('#mask').fadeTo("fast",0.8);    
    var winH = $(window).height();  
    var winW = $(window).width();  
    $(id).css('top',  winH/2-$(id).height()/2);  
    $(id).css('left', winW/2-$(id).width()/2);  
    $(id).show();
   	var insertBar = '<img src="/GoldCoastImages/ajax-loader-bar.gif" alt="Processing...please wait."/>';
   	$('#dialog .theBar').append(insertBar);
   	return false;
}
