var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

$(document).ready(function()
{	$('#genres > li').bind('mouseover', jsddm_open);
	$('#genres > li').bind('mouseout',  jsddm_timer);});

document.onclick = jsddm_close;

function getMore(howMany,total)
{
    document.getElementById('loadimg').style.display = 'block';
    $.get("/admin/Postback/moreResults.aspx?many="+howMany,function (data){
    
        $('#song1').append(data);
        document.getElementById('shownresults').value = (parseInt(howMany) + 10);
        
        if ((parseInt(howMany)+10) >= parseInt(total))
        {
            $('.moreresults').css('display','none');
        }
        
        $(".rating").stars({
                    cancelShow: false,
                    inputType: "select"
              });

        
        document.getElementById('loadimg').style.display = 'none';
    
    });
    
    
}

function getMoreMix(howMany,total)
{
    document.getElementById('loadimg').style.display = 'block';
    $.get("/admin/Postback/moreResultsMix.aspx?many="+howMany,function (data){
    
        $('#song1').append(data);
        document.getElementById('shownresults').value = (parseInt(howMany) + 10);
        
        if ((parseInt(howMany)+10) >= parseInt(total))
        {
            $('.moreresults').css('display','none');
        }
        
        $(".rating").stars({
                    cancelShow: false,
                    inputType: "select"
              });

        
        document.getElementById('loadimg').style.display = 'none';
    
    });
    
    
}

function getMoreGenre(howMany,total,thetype,genreid)
{
    document.getElementById('loadimg').style.display = 'block';
    $.get("/admin/Postback/moreResultsGenre.aspx?many="+howMany+"&type="+thetype+"&genreid="+genreid,function (data){
    
        $('#song1').append(data);
        document.getElementById('shownresults').value = (parseInt(howMany) + 10);
        
        if ((parseInt(howMany)+10) >= parseInt(total))
        {
            $('.moreresults').css('display','none');
        }
        
        $(".rating").stars({
                    cancelShow: false,
                    inputType: "select"
              });

        
        document.getElementById('loadimg').style.display = 'none';
    
    });
    
    
}

function countDownload(songKey)
{
    $.get('/admin/Postback/countDL.aspx?songkey=' + songKey);
}

function countDownloadMix(mixKey)
{
    $.get('/admin/Postback/countDLMix.aspx?mixkey=' + mixKey);
}

function reTweet(Username,songKey)
{
    if (document.getElementById('tloggedin').value != '')
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Retweeting just a moment...</h2>' });
        $.get("/admin/Postback/reTweet.aspx?username="+Username+'&songkey='+songKey+'&ID='+document.getElementById('tloggedin').value,function (data){
            $.unblockUI();
            if (data == '1')
            {
               //success!
                jAlert('Thanks for retweeting!','Thanks!');
            }
            else{
                jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }else{
        jAlert('You must be logged in to retweet','Please login');
    }
}

function reTweetMix(Username,mixKey)
{
    if (document.getElementById('tloggedin').value != '')
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Retweeting just a moment...</h2>' });
        $.get("/admin/Postback/reTweetMix.aspx?username="+Username+'&mixkey='+mixKey+'&ID='+document.getElementById('tloggedin').value,function (data){
            $.unblockUI();
            if (data == '1')
            {
               //success!
                jAlert('Thanks for retweeting!','Thanks!');
            }
            else{
                jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }else{
        jAlert('You must be logged in to retweet','Please login');
    }
}


function follow(Username)
{
    if (document.getElementById('tloggedin').value != '')
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Trying to follow '+Username+' just a moment...</h2>' });
        $.get("/admin/Postback/follow.aspx?username="+Username+'&ID='+document.getElementById('tloggedin').value,function (data){
            $.unblockUI();
            if (data == '1')
            {
               //success!
                jAlert('You are now following '+Username+'!','Now Following!');
            }
            else{
                jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }else{
        jAlert('You must be logged in to follow','Please login');
    }
}

function comment(Username,songkey,lastid)
{
    var posttext = document.getElementById('comment1').value;
    
    //Fix for Pound Sign
    posttext = posttext.replace('#','[*Pound*]')
    
    if (document.getElementById('tloggedin').value != '' && posttext != '')
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Posting your comment just a moment...</h2>' });
        $.get("/admin/Postback/postcomment.aspx?username="+Username+'&SongKey='+songkey+'&comment='+encodeURI(posttext)+'&ID='+document.getElementById('tloggedin').value,function (data){
            if (data == '1')
            {
                 $.get("/admin/Postback/updatecomments.aspx?songkey="+songkey+"&id="+lastid,function (data){
                    $('#allcomments').prepend(data);
                   document.getElementById('comcount').innerHTML = (parseInt(document.getElementById('comcount').innerHTML) + 1);
                   document.getElementById('comment1').value = '';
                   //success!
                   $.unblockUI();
                    jAlert('Thanks for commenting!','Thanks!');
                });
                
            }
            else{
                $.unblockUI();
                jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }else{
        if(document.getElementById('tloggedin').value == '')
        {
            jAlert('You must be logged in to comment','Must be logged in');
        }
        else
        {
            jAlert('Did you forget the comment?','Any Comment?');
        }
    }
}

function commentMix(Username,Mixkey,lastid)
{
    var posttext = document.getElementById('comment1').value;
    
    //Fix for Pound Sign
    posttext = posttext.replace('#','[*Pound*]')
    
    if (document.getElementById('tloggedin').value != '' && posttext != '')
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Posting your comment just a moment...</h2>' });
        $.get("/admin/Postback/postcommentMix.aspx?username="+Username+'&mixkey='+Mixkey+'&comment='+encodeURI(posttext)+'&ID='+document.getElementById('tloggedin').value,function (data){
            if (data == '1')
            {
                 $.get("/admin/Postback/updatecommentsMix.aspx?mixkey="+Mixkey+"&id="+lastid,function (data){
                    $('#allcomments').prepend(data);
                   document.getElementById('comcount').innerHTML = (parseInt(document.getElementById('comcount').innerHTML) + 1);
                   document.getElementById('comment1').value = '';
                   //success!
                   $.unblockUI();
                    jAlert('Thanks for commenting!','Thanks!');
                });
                
            }
            else{
                $.unblockUI();
                jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }else{
        if(document.getElementById('tloggedin').value == '')
        {
            jAlert('You must be logged in to comment','Must be logged in');
        }
        else
        {
            jAlert('Did you forget the comment?','Any Comment?');
        }
    }
}

function commentProfile(Username,UserID,lastid)
{
    var posttext = document.getElementById('comment1').value;
    
    //Fix for Pound Sign
    posttext = posttext.replace('#','[*Pound*]')
    
    if (document.getElementById('tloggedin').value != '' && posttext != '')
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Posting your comment just a moment...</h2>' });
        $.get("/admin/Postback/postcommentProfile.aspx?username="+Username+'&userkey='+UserID+'&comment='+encodeURI(posttext)+'&ID='+document.getElementById('tloggedin').value,function (data){
            if (data == '1')
            {
                 $.get("/admin/Postback/updatecommentsProfile.aspx?userkey="+UserID+"&id="+lastid,function (data){
                    $('#allcomments').prepend(data);
                   document.getElementById('comcount').innerHTML = (parseInt(document.getElementById('comcount').innerHTML) + 1);
                   document.getElementById('comment1').value = '';
                   //success!
                   $.unblockUI();
                    jAlert('Thanks for commenting!','Thanks!');
                });
                
            }
            else{
                $.unblockUI();
                jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }else{
        if(document.getElementById('tloggedin').value == '')
        {
            jAlert('You must be logged in to comment','Must be logged in');
        }
        else
        {
            jAlert('Did you forget the comment?','Any Comment?');
        }
    }
}

function commentPic(Username,PicID,lastid)
{
    var posttext = document.getElementById('comment1').value;
    
    //Fix for Pound Sign
    posttext = posttext.replace('#','[*Pound*]')
    
    if (document.getElementById('tloggedin').value != '' && posttext != '')
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Posting your comment just a moment...</h2>' });
        $.get("/admin/Postback/postcommentPicture.aspx?username="+Username+'&pictureid='+PicID+'&comment='+encodeURI(posttext)+'&ID='+document.getElementById('tloggedin').value,function (data){
            if (data == '1')
            {
                 $.get("/admin/Postback/updatecommentsPicture.aspx?pictureid="+PicID+"&id="+lastid,function (data){
                    $('#allcomments').prepend(data);
                   document.getElementById('comcount').innerHTML = (parseInt(document.getElementById('comcount').innerHTML) + 1);
                   document.getElementById('comment1').value = '';
                   //success!
                   $.unblockUI();
                    jAlert('Thanks for commenting!','Thanks!');
                });
                
            }
            else{
                $.unblockUI();
                jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }else{
        if(document.getElementById('tloggedin').value == '')
        {
            jAlert('You must be logged in to comment','Must be logged in');
        }
        else
        {
            jAlert('Did you forget the comment?','Any Comment?');
        }
    }
}

function starVote(rating,ip,songKey)
{
    $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Submitting your vote just a moment...</h2>' });
    $.get("/admin/Postback/vote.aspx?rating="+rating+'&songkey='+songKey+'&IP='+ip,function (data){
    $.unblockUI();
    if (data.indexOf("ERROR") == -1)
    {
        //success!
        $("#star1").stars("select", data);
        jAlert('Your vote of '+rating+' stars was received, with your vote the rating is now: '+data+' stars','Rating Recorded!');
    }
    else{
        alert(data);
    }
    });
    
}

function starVoteMix(rating,ip,MixKey)
{
    $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Submitting your vote just a moment...</h2>' });
    $.get("/admin/Postback/voteMix.aspx?rating="+rating+'&mixkey='+MixKey+'&IP='+ip,function (data){
    $.unblockUI();
    if (data.indexOf("ERROR") == -1)
    {
        //success!
        $("#star1").stars("select", data);
        jAlert('Your vote of '+rating+' stars was received, with your vote the rating is now: '+data+' stars','Rating Recorded!');
    }
    else{
        alert(data);
    }
    });
    
}

function deleteSong(songID,result)
{
    if (result == true){
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Deleting your song just a moment...</h2>' });
        $.get("/admin/Postback/deleteSong.aspx?songid="+songID,function (data){
        $.unblockUI();
        if (data.indexOf("ERROR") == -1)
        {
            //success!
            jAlert('Your song has been deleted.','Song Deleted!',function(r) {window.location.reload()});
        }
        else{
            jAlert(data,'Error!');
        }
        });
    }
}

function deleteMix(mixID,result)
{
    if (result == true){
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Deleting your Mixtape just a moment...</h2>' });
        $.get("/admin/Postback/deleteMixtape.aspx?mixtapeid="+mixID,function (data){
        $.unblockUI();
        if (data.indexOf("ERROR") == -1)
        {
            //success!
            jAlert('Your mixtape has been deleted.','Mixtape Deleted!',function(r) {window.location.reload()});
        }
        else{
            jAlert(data,'Error!');
        }
        });
    }
}

function editMixSong(songID)
{
    var songTitle = document.getElementById('Title').value;
    songTitle = songTitle.replace('#','[*Pound*]')
    
    if(songTitle==''){
        jAlert('Song Title is required!','Please fill in all fields');
    }else{
        $.get("/admin/Postback/performUpdateSong.aspx?songid="+songID+"&songtitle="+encodeURI(songTitle)+"&songartist=&genre=0&dl=false",function (data){
            $.unblockUI();
            if (data == '1')
            {
                //success!
                jAlert('Your song has been updated.','Song Updated!',function(r) {parent.window.location.reload()});
            }
            else{
                jAlert(data,'Error!');
            }
        });
    }
}

function editMixTape(MixID)
{
    var songTitle = document.getElementById('Title').value;
    songTitle = songTitle.replace('#','[*Pound*]')
    var songArtist = document.getElementById('Artist').value;
    songArtist = songArtist.replace('#','[*Pound*]')
    var genre = document.getElementById('genre1').value;
    var dl = document.getElementById('downloadable').checked;
    
    if(songTitle==''||songArtist==''||genre==''){
        jAlert('All fields are required!','Please fill in all fields');
    }else{
        $.get("/admin/Postback/performUpdateMixtape.aspx?mixtapeid="+MixID+"&title="+encodeURI(songTitle)+"&artist="+encodeURI(songArtist)+"&genre="+genre+"&dl="+dl,function (data){
            $.unblockUI();
            if (data == '1')
            {
                //success!
                jAlert('Your song has been updated.','Song Updated!',function(r) {parent.window.location.reload()});
            }
            else{
                jAlert(data,'Error!');
            }
        });
    }
}

function editSong(songID)
{
    var songTitle = document.getElementById('Title').value;
    songTitle = songTitle.replace('#','[*Pound*]')
    var songArtist = document.getElementById('Artist').value;
    songArtist = songArtist.replace('#','[*Pound*]')
    var genre = document.getElementById('genre1').value;
    var dl = document.getElementById('downloadable').checked;
    
    if(songTitle==''||songArtist==''||genre==''){
        jAlert('All fields are required!','Please fill in all fields');
    }else{
        $.get("/admin/Postback/performUpdateSong.aspx?songid="+songID+"&songtitle="+encodeURI(songTitle)+"&songartist="+encodeURI(songArtist)+"&genre="+genre+"&dl="+dl,function (data){
            $.unblockUI();
            if (data == '1')
            {
                //success!
                jAlert('Your song has been updated.','Song Updated!',function(r) {parent.window.location.reload()});
            }
            else{
                jAlert(data,'Error!');
            }
        });
    }
}

function dlcomplete(a, b, c, d, e) 
{
    if (d==''){
        alert('We are sorry, there has been an error uploading your song, please try again.');
    }else{
        document.getElementById("flashupload1").style.display = 'none';
        document.getElementById("uploaded1").style.display = 'block';
        document.getElementById("tempid").value = d;
        document.getElementById("upload1").className = 'upload';
        document.getElementById("upload1").title = 'Tweet Your Song';
        document.getElementById("upload1").disabled = false;
    }
}

function submitmixstep1()
{
    $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Submitting Step 1 of 3...</h2>' });
}

function submitmixstep2()
{
    $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Submitting Step 2 of 3...</h2>' });
}

function submitmixstep3()
{
    $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Tweeting Your Mixtape/Album...</h2>' });
}

function mixtapeTrackUploaded(d)
        {
            var data = 'Track '+d+' uploaded successfully!<br/>'
            $('#uploadlist').prepend(data);
        }
function sendDM(Username)
{
   var Msg = document.getElementById('msg').value;
   Msg = Msg.replace('#','[*Pound*]')
   
   if (Msg == '')
   {
    top.jAlert('Please enter a message','Error!');
   }
   else{
       $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/smajax.gif" /></p><h2>Sending...</h2>' });
       $.get("/admin/Postback/sendDM.aspx?username="+Username+'&userid='+top.document.getElementById('tloggedin').value+'&&msg='+encodeURI(Msg),function (data){
            $.unblockUI();
            if (data == '1')
            {
                //success!
                top.closeBox();
                top.jAlert('Your message has been sent to '+Username+'!','Message Sent!');
            }
            else{
                top.jAlert('Twitter Error: '+data,'Error!');
            }
        });
    }
}

function closeBox()
{
    $.fn.colorbox.close()
}

function doingPic()
{
    if (document.getElementById('picname').value == '')
    {
        jAlert('Please enter a picture name','Error');
        return false;
    }
    else if (document.getElementById('pic1').value == '')
    {
        jAlert('Please select a picture for upload','Error');
        return false;
    }
    else if (document.getElementById('pic1').value.toLowerCase().indexOf('.jpg') == -1 && document.getElementById('pic1').value.toLowerCase().indexOf('.png') == -1 && document.getElementById('pic1').value.toLowerCase().indexOf('.gif') == -1) 
    {
        jAlert('Please select a valid picture: <br /> <strong>Only *.jpg, *.png, *gif files are allowed</strong>','Error');
        return false;
    }
    else
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/smajax.gif" /></p><h2>Uploading...</h2>' });
        return true;
    }
}

function picSuccess()
{
    parent.closeBox(); 
    parent.refreshImages();
}

function refreshImages()
{
    $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Refreshing Profile...</h2>' }); 
    window.location.reload()
}

function deletePic(picID, result)
{
    if (result == true){
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/ajax-loader.gif" /></p><h2>Deleting your picture just a moment...</h2>' });
        $.get("/admin/Postback/deletePicture.aspx?pictureid="+picID,function (data){
        $.unblockUI();
        if (data.indexOf("ERROR") == -1)
        {
            //success!
            jAlert('Your picture has been deleted.','Picture Deleted!',function(r) {window.location.reload()});
        }
        else{
            jAlert(data,'Error!');
        }
        });
    }
}

function setStatus()
{
    if (document.getElementById('msg').value == '')
    {
        jAlert('Please enter a Status','Error');
        return false;
    }
    if (document.getElementById('website').value != '' && isURL(document.getElementById('website').value) != true)
    {
        jAlert('Your official website is invalid! <br /><i>*remember the http:// </i>','Error');
        return false;
    }
    else
    {
        $.blockUI({ message: '<p class="uimsg"><img src="/frontpages/images/smajax.gif" /></p><h2>Setting Status...</h2>' });
        return true;
    }
}


function isURL(urlStr) {
if (urlStr.indexOf(" ") != -1) {
alert("Spaces are not allowed in a URL");
return false;
}

if (urlStr == "" || urlStr == null) {
return true;
}

urlStr=urlStr.toLowerCase();

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var atom=validChars + '+';
var urlPat=/^http:\/\/(\w*)\.([\-\+a-z0-9]*)\.(\w*)/;
var matchArray=urlStr.match(urlPat);

if (matchArray==null) {
//alert("The URL seems incorrect \ncheck it begins with http://\n and it has 2 .'s");
return false;
}

var user=matchArray[2];
var domain=matchArray[3];

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
//alert("This domain contains invalid characters.");
return false;
}
}

for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i) > 127) {
//alert("This domain name contains invalid characters.");
return false;
}
}

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;

for (i=0;i<len;i++) {
if (domArr[i].search(atomPat) == -1) {
//alert("The domain name does not seem to be valid.");
return false;
}
}

return true;
} 


function update_user_box() 
    { 
    //var user_box = document.getElementById("user"); 
    //user_box.innerHTML =  "<img src='/frontpages/images/facebookcheck.jpg' alt='' /> Facebook account linked!";
    window.location = "/Facebook-Integration.htm?mode=perm";
    FB.XFBML.Host.parseDomTree();
}

function twitterlogin()
{
    window.open('/admin/popups/popup_oAuth.aspx','','toolbars=no,scrollbars=yes,status=yes,location=no,width=800,height=600,resizable=yes,left=10,top=10,postionx=10,positiony=10');

}

function closeForm(){
	window.close();
}