// JavaScript Document
// this variable is required for lightboxes that pop up onLoad
autoLoadbox = "";

function addSlashes(str) {
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\0/g,'\\0');
    return str;
}

function returnString() {
    alert("Returning hello world");
    return "hello world";
}
var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
        {
            string: navigator.userAgent,
            subString: "Chrome",
            identity: "Chrome"
        },
        {   string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari",
            versionSearch: "Version"
        },
        {
            prop: window.opera,
            identity: "Opera"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {       // for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "IE",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {       // for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS : [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
               string: navigator.userAgent,
               subString: "iPhone",
               identity: "iPhone/iPod"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]

};

BrowserDetect.init();

function getBrowser() {
    var result = 'unknown';
    if (BrowserDetect.browser) {
        result = BrowserDetect.browser;
    }
    if (BrowserDetect.version) {
        result += ' ' + BrowserDetect.version;
    }
    return result;
}

function formatNumber(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1,$2');
    }
    return x1 + x2;
}


function close_popup(obj) {
    $(obj).hide();
}

function openPopup(handle, isModal)
{
    if (isModal === null || isModal === undefined) {
        isModal = true;
    }
    var popup = jQuery('.popups').find('.' + handle);
    if (popup.length < 1)
    {
        return;
    }
    closePopups(isModal);
    if (isModal) {
        jQuery('.modal-overlay').show();
    }
    else {
        jQuery('.modal-overlay').hide();
    }
    
    popup.show();
    var marginLeft = Math.floor(popup.width() / 2) * -1;
    var marginTop = Math.floor(popup.height() / 2) * -1;
    popup.css('margin-left', marginLeft + 'px');
    popup.css('margin-top', marginTop + 'px');
}

function closePopups(ignoreModal)
{
    jQuery('.popups').children('*').hide();
    if (!ignoreModal) {
        jQuery('.modal-overlay').hide();
    }
}

function openLoadingPopup()
{
    openPopup('loadingWrapper');
}

function closeLoadingPopup()
{
    closePopups();
}

function closeWindow() {
    setTimeout(function(){
        if (window.console) window.console.log("window.close()");
        window.close();
    },500);
}

function showSubNav(id) {
    var div = jQuery('#pagewrapper').find('#sub_' + id);
    div.show();
}

function hideSubNav(id) {
    var div = jQuery('#pagewrapper').find('#sub_' + id);
    div.hide();
}

function downloadScattertunes(swfUrl)
{
	openGenericWidget(null, true);
}

// Handles the homepage more-info button clicks
function moreinfo(trackId, albumId, moreInfoUrl) {
    if (moreInfoUrl) {
        window.location.href = moreInfoUrl;
        return;
    }

    var cmd = {
        albumId:albumId,
        productId:trackId,
        searchTerm:'WEB PAGE',
        cmd:'VIEW'
    };
    
//    var cmd = '{"albumId": "61f846c0-f66e-4bb5-aa8a-a72698140d75", "productId": "be7441eb-1155-4d7f-830e-14da3d607500",  "searchTerm": "(Remember Me) Im the One Who Loves You",  "cmd": "VIEW"}';   
    var flashvars = 'cmd=' + encodeURI(cmd);
    var params = {
        wmode:'transparent', 
        flashvars:flashvars
    };
    var attributes = {};
    var replaceId = 'iBoardLauncherPopup';
    
    openGenericWidget(cmd, true);
}
// Handles the homepage more-info button clicks
function valbumMoreinfo(upc, searchTerm) {
        var moreInfoUrl = '/valbums/?upc=' + upc;
        window.location.href = moreInfoUrl;
        return;
}

function iBoardLaunchPopup_hide()
{
    closeWidget();
}

// Makes the homepage popups
function popup(obj) {	
    $$('.top10_popup').invoke('hide');

//	document.getElementById("newexclusives_popup").style.visibility="hidden";
//	document.getElementById("onrise_popup").style.visibility="hidden";
//	document.getElementById("hottest_popup").style.visibility="hidden";

	var obj = jQuery('#' + obj);
	var parentDiv = jQuery(document.getElementById("pagewrapper"));
	var xpos, ypos = 0;
	
	if (obj.id=='FEATURE_popup') {
		var bn = $('bn_fboxtop10valbums');
		xpos = parentDiv.width() - 30;
		ypos = 50;
		}
	else {
		xpos = parentDiv.width();
		ypos = document.body.offsetTop+130;
	}
    obj.show();
	obj.css('left', (xpos - obj.width()) + "px");
	obj.css('top', ypos + "px");
}


function validateContactForm(formName) {
	var formElems = document.getElementById(formName).elements;
	var $errors = 0;
	var addr = formElems["email"].value;
	var snailpos = addr.indexOf("@",0);
	
	for (var i=0; i<formElems.length; i++){
			if (formElems[i].name!="phone" && formElems[i].value==""){
			alert (formElems[i].name + " is required.");
			//$errors+=1;
			return false;
			}
		
			else {
			
			if (snailpos <=0) {
				alert ("Your email address appears to be invalid.")
				return false;
			}	
		}	
	}
	return true;
}

// mouseovers 
function rollover(filename) {
	var pic = filename + "_on.gif";	
	this.src=pic;
}

function rollout(filename) {
	var pic = filename + ".gif";
	this.src=pic;
}

function feedback(firstName,lastName,emailAddress,phone,subject,comments)
{
	new Ajax.Request('/WebService/Service.asmx/ContactUs_SupportPage', 
		{
			method: 'post',
			postBody: "firstName=" + escape(firstName) +
				"&lastName=" + escape(lastName) +
				"&email=" + escape(emailAddress) +
				"&phone=" + escape(phone) + 
				"&subject=" + escape(subject) +
				"&comments=" + escape(comments)
		});
				
}

function isMaximized()
{

	if (window.outerHeight != null)
	{
		//FF
		if((document.getElementsByTagName('body')[0].style.overflow = 'scroll') && (screen.width - window.outerWidth == 17))
		{
			//alert('FF');
			return true;
		}
		else if (screen.width - window.outerWidth == -8)
		{
			//alert('IE');
			return true;
		}
	}
	else
	{
		//IE 7.0
		if (document.body.scroll == 'no')
		{
			//alert('IE');
			return true;
		}
		else if((document.getElementsByTagName('body')[0].style.overflow = 'scroll') && (screen.width - window.outerWidth == 21))
		{
			//alert('FF');
			return true;
		}
		else if (screen.width - document.body.clientWidth == 4)
		{
			//alert('IE');
			return true;
		}
	
	}
	
	return false;
}

ScatterTunes = function()
{
    return {
        notify: function(page,detail) {
            new Ajax.Request('/WebService/SiteService.asmx/Notify', {
                contentType: "application/json; charset=utf-8",
                method:'post',
                postBody:JSON.stringify({
                    id: null,
                    channel: "WebSite",
                    page: page,
                    type: "P",
                    detail: detail
                })
            });
        },
        
        notifyInstall: function(airStatus) {
            new Ajax.Request('/WebService/SiteService.asmx/Notify', {
                contentType: "application/json; charset=utf-8",
                method:'post',
                postBody:JSON.stringify({
                    id: null,
                    channel: "WebSite",
                    page: "Get ScatterTunes",
                    type: "C",
                    detail: JSON.stringify({type:'InstallClick', airStatus:airStatus})
                })
            });
        }
    }
}();