﻿/// <reference path="jquery-1.4.2.js" />
/// <reference path="jquery.scrollTo-1.4.2.js" />
/// <reference path="jquery.selectbox-1.2.js" />
/// <reference path="tiny_mce/tiny_mce.js" />
/// <reference path="tabs.js" />
/// <reference path="jquery.tweet.js" />
/// <reference path="jquery.fileinput.js" />


String.prototype.fromPxToInt = function () {
	var val = parseInt(this.toString().replace('px', ''));
	if (isNaN(val))
		return 0;
	else
		return val;
};

$(function () {

    $('a.nofollow').attr("rel", "nofollow");

    // This will make all the external url links in the Menu Navigation open in a new window.
    $("nav a[href^='http://']").attr("target", "_blank");

    // remove annoying dotted box around links when clicked
    $('a, button').click(function () { $(this).blur(); });

    // mark first & last
    $('.mark-last').each(function () {
        var $children = $(this).children();
        $children.last().addClass('last');
        //$children.first().addClass('first');
    });

    // has sub-menu
    $('.sub-menu').each(function () {
        $(this).parent('li').addClass('has-sub-menu');
    });

    $('div.field').each(function (index) {
        var $newZIndex = 150 - index;
        $(this).css("z-index", $newZIndex);
    });

    // select boxes
    $('form select.custom').selectbox();

    // tinymce on marked textareas
    tinyMCE.init({
        mode: "textareas",
        editor_selector: "mceEditor",
        theme: 'advanced',
        theme_advanced_buttons1: 'bold,italic,underline, strikethrough, separator,justifyleft, justifycenter,justifyright,  justifyfull, separator,help',
        theme_advanced_buttons2: '',
        theme_advanced_buttons3: '',
        theme_advanced_buttons4: '',
        theme_advanced_toolbar_location: 'top',
        theme_advanced_toolbar_align: 'left',
        setup: function (ed) {
            ed.onKeyPress.add(function (ed, e) {
                //console.debug($(ed).parents('.field.comment'));
            });
        }
    });


    // --- Feed Tabs ---
    $('ul.feed.tabs').each(function () {
        var $me = $(this);

        // maintain currently selected item
        var $currentItems = $me.find('li a.current');
        var currentIndex = 0;
        if ($currentItems.length > 0)
            currentIndex = $currentItems.parent().index();

        $(this).tabs('.panes>*', { initialIndex: currentIndex });
    });
    $('ul.feed.tabs a')
		  .click(function () {
		      $(this).blur();
		  });


    // --- Artist Discography Tabs ---
    $('.discography>.record .tabs')
		.each(function () {
		    $(this).tabs('.panes>.pane');

		    var $record = $(this).parents('.record');
		    var $reviewFullEl = $record.find('.review-full');
		    var $reviewIntroEl = $record.find('.review-intro');
		    var $tabs = $(this).children();
		    var $reviewTab = $(this).children('.review');
		    var $showHideLink = $record.find('a.show-hide-text');

		    var $trackListTab = $(this).children('.track-list');
		    var $trackListPane = $record.find('.tracks-pane');
		    var $singlesTab = $(this).children('.singles');
		    var $singlesPane = $record.find('.singles-pane');

		    $singlesTab.click(function () {
		        if ($singlesPane.css('display') == 'block') {
		            $singlesPane.hide();
		            $(this).removeClass('current');
		        }
		        else {
		            $singlesPane.show();

		            if (!$(this).hasClass('current'))
		                $(this).addClass('current');
		        }
		        return false;
		    });

		    $trackListTab.click(function () {
		        if ($trackListPane.css('display') == 'block') {
		            $trackListPane.hide();
		            $(this).removeClass('current');
		        }
		        else {
		            $trackListPane.show();

		            if (!$(this).hasClass('current'))
		                $(this).addClass('current');
		        }
		        return false;
		    });

		    $reviewTab.click(function () {
		        if ($reviewFullEl.css('display') == 'block') {
		            $reviewIntroEl.show();
		            $reviewFullEl.hide();
		            $(this).removeClass('current');
		            $showHideLink.text('Read More');
		        }
		        else {
		            $reviewFullEl.show();
		            $reviewIntroEl.hide();

		            if (!$(this).hasClass('current'))
		                $(this).addClass('current');

		            $showHideLink.text('Hide Text');
		        }
		        return false;
		    });

		    $showHideLink.click(function () {
		        $reviewTab.click();
		        return false;
		    });
		});

    // --- Twitter ---

    $('.getting-social .pane.twitter')
		.tweet({
		    username: 'CMC_Australia',
		    join_text: '',
		    avatar_size: 32,
		    count: 4,
		    loading_text: 'loading tweets...',
		    relativeTime: false
		});
    $('.getting-social .pane.twitter ul li:last')
		.addClass('last');


    // --- Sub-Tables ---

    $('.sub-table').each(function () {
        var $subtable = $(this);
        $subtable
			.prev()
			.find('a.show-sub-table')
			.click(function () {
			    var $tr = $(this).parents('tr');
			    if ($subtable.css('display') == 'none') {
			        $subtable.css('display', 'table-row');
			        $tr.addClass('expanded');
			    }
			    else {
			        $subtable.css('display', 'none');
			        $tr.removeClass('expanded');
			    }
			    return false;
			});
    });


    // --- Popups ---

    // show-popup
    $('a.show-popup').click(function () {
        var linkPos = $(this).offset();
        var $popup = $('.popup.' + $(this).attr('href').replace('#', ''));
        $popup
			.css('left', (linkPos.left + 10) + 'px')
			.css('top', (linkPos.top - $popup.height()) + 'px')
			.show();
        return false;
    });

    // hide popup
    $('.popup').click(function () {
        $(this).hide();
        return false;
    });

    $(document.body).click(function () {
        $('.popup').hide();
    });


    // --- File Upload ---
    $('input[type=file]').customFileInput();


    // --- 'Just Played' ticker ---
    if ($('.just-played ul').length == 1) {
        $.fn.totalWidth = function () {
            var width = 0;
            $(this).each(function (index, item) {
                width += $(item).width();
            });
            return width;
        };

        var $justPlayedUl = $('div.just-played ul');
        var $song = $('div.just-played .song');

        $justPlayedUl.mouseover(function () {
            $song.stop();
        });

        $justPlayedUl.mouseout(function () {
            scrollToEnd();
        });

        var $justPlayedLis = $justPlayedUl.children();
        var cycleWidth = $justPlayedLis.totalWidth();
        $justPlayedUl.append($justPlayedLis.html());
        function scrollToEnd() {
            var speed = (30 * ((cycleWidth - $song[0].scrollLeft) / 2));
            $song
				.scrollTo(cycleWidth, speed, {
				    easing: 'linear',
				    onAfter: function () {
				        $song.scrollTo(0, 0);
				        scrollToEnd();
				    }
				});
        }
        scrollToEnd();
    }

});

//Flash Switcher
function spawnWindow(URL, Name, features) {
    window.open(URL, Name, features);
}
