/* base Javascript for XFM */

/*
 * all input type=text with a class of 'use_label_as_value' will have their 
 * value set as the label, which clears when clicked
 */
jQuery(function() {
    jQuery('.use_label_as_value')
    .each(function(){
        jQuery(this)
        .attr('value', jQuery('label[for="' + jQuery(this).attr('id') + '"]').html());
        this.defaultvalue = jQuery(this).attr('value');
        jQuery(this)
        .focus(function(e){
            if(jQuery(this).attr('value') == this.defaultvalue) {
                jQuery(this).attr('value', '');
                this.defaultvalue = '';
            }
        });
    });
});
/* launch player stuff */
jQuery(function(){
	// the class the script is looking for
	var player_class = '.launch_player';
	// function used to open the player
	function openPlayer(url) {
		if (typeof url == 'undefined') url = "http://mediaweb.musicradio.com/player/default.asp?s=69&e=0";
		try {
		    playerWin = window.open(correctStationVar(url),'playerWin','width=607,height=558,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1');
	    }
	    catch (error) {
	        // ignore the IE bug
	    }
	};
	function correctStationVar(url){
        if (typeof region.mediamanager_station == 'number') {
            station_id = region.mediamanager_station;
            stationVar = new RegExp('s=[0-9]+')
            url = url.replace(stationVar, "s=" + station_id);
        }
        return url;
    };
	// looping through each link and assigning the launch function to it
	$(player_class).each(function(){
		$(this).click(function() {
			openPlayer($(this).attr('href'));
			return false;
		});
	});
}
)

/*
 * Launch "external" links in a new window
 */
jQuery(function() {
   jQuery('a.external')
   .each(function() {
       jQuery(this).attr('target', '_blank');
   });
});

/*
 * Modal login dialogue for XFM VIP
 */
jQuery(function() {
    jQuery('#login_link').click(function() {
        var myLoginString = gcap.cookie.readCookie('eLS5893');
        var myEmail = '';
        if (myLoginString) {
            if (myLoginString.indexOf('LoginName') > 0) {
                myEmail = myLoginString.replace(/^(.*)LoginName=([a-zA-Z0-9-\/%]+)(.*)$/, '$2');
            }
        }
        if (region.current_region == 'London') {
            vip_select = '<option value="london" selected="selected">Xfm London \/ DAB<\/option><option value="manchester">Xfm Manchester<\/option>';
            stationIDlist = "5893,6365";
        }
        else {
            vip_select = '<option value="manchester" selected="selected">Xfm Manchester<\/option><option value="london">Xfm London \/ DAB<\/option>';
            stationIDlist = "6365,5893";
        }
        var formString = '<div id="user_login">\
            <div class="hd">\
                <h2>Login to xfm+<\/h2>\
                <div class="region_picker">\
                <\/div>\
            <\/div>\
            <div class="bd">\
                <form id="vip_login" method="post" action="http:\/\/www.xfm.co.uk\/els\/elsLoginPrc.asp">\
                    <div>\
                        <label for="login_username">Email<\/label>\
                        <input type="text" name="fldUserName" id="login_username" value="' + unescape(myEmail) + '">\
                    <\/div>\
                    <div>\
                        <label for="login_password">Password<\/label>\
                        <input type="password" name="fldPassword" id="login_password">\
                    <\/div>\
                    <div class="remember">\
                        <input type="checkbox" class="checkbox" id="chkRememberMe" name="fldSaveLogin" value="1">\
                        <label for="chkRememberMe">remember me<\/label>\
                    <\/div>\
                    <div class="btn">\
                        <input type="submit" class="login" value="Login to xfm+">\
                    <\/div>\
                    <button type="button" id="cancel_login">Cancel<\/button\>\
                    <p class="login-options"><a href="\/els\/elsLoginFrm.asp?forgot=1">Forgot your password?<\/a> | <a href="http:\/\/www.xfm.co.uk/els/elsSignupFrm.asp?newmember=1&amp;register=1">Register<\/a>\
                    <\/p>\
                    <input type="hidden" name="Action" value="">\
                    <input type="hidden" value="' + window.location.href + '" name="fldDestination">\
                    <input type="hidden" name="fldStationIDList" value="' + stationIDlist + '">\
                <\/form>\
            <\/div>\
        <\/div>';
        jQuery(formString).modal({overlay:50});
        jQuery('#modalContainer').css('margin-top', '-' + (jQuery('#modalContainer').height() / 2) + 'px');
        jQuery('#cancel_login').click(function(){
            jQuery.modal.close();
            return false;
        });
        return false;
    });
});
