var ratingDivID = "";
var usernameControlId = "hfUsername";
var username2ControlId = "";
var assetControlId = "hfAssetId";
var commentsButtonControlId = "";
var embedCodeHiddenField = "hfEmbedCode";

function setHiddenEmbedTagId(fldId){
    embedCodeHiddenField = fldId;
}

function setRatingDivId(id) {
    ratingDivID = id;
}

function setUsernameControlId(id) {
    if(usernameControlId == "") usernameControlId = id;
}

function setUsername2ControlId(id) {
    if(username2ControlId == "") username2ControlId = id;
}

function setAssetIdControlId(id,id2) {
    assetControlId = id;
}

function setCommentsButtonId(id) {
    commentsButtonControlId = id;
}

function showTimeAgo(serverDate,itemDate) {
    var clientDate = new Date();
	var serverClientDiff = clientDate - serverDate;
	var diff = (serverDate - itemDate) + serverClientDiff;
	return time(diff);
}

function two(x) {return ((x>9)?"":"")+x}

function three(x) {return ((x>99)?"":"")+((x>9)?"":"")+x}

function time(ms) {
    var t = "";
    var s = "";
    var sec = Math.floor(ms/1000)
    ms = ms % 1000
    //t = three(ms)
    var min = Math.floor(sec/60)
    sec = sec % 60
    //t = two(sec) + ":" + t
    var hr = Math.floor(min/60)
    min = min % 60
    //t = two(min) + ":" + t
    var day = Math.floor(hr/60)
    hr = hr % 60
    //t = two(hr) + ":" + t
    //t = day + ":" + t
  
    if(parseInt(hr)> 0) {
    if (hr>1) s = "s";
        t = hr + " hour" + s;
    if(min > 0) {
    	if (min>1) s = "s";
    	t = t + " " + min + " minute" + s;
    }
    t = t + " ago";
    } else if (parseInt(min)>0) {
    if (min>1) s = "s";
        t = min + " minute" + s + " ago";
    } else {
    if (sec>1) s = "s";
        t = sec + " second" + s + " ago";
    }
	t = " (" + t + ")";
    return t;
}


function setUsernameValue() {
    var value = readCookie();
    if(usernameControlId != "") {
        if(document.getElementById(usernameControlId) != null) {
             document.getElementById(usernameControlId).value = value;
        }
    }
    if(username2ControlId != "") {
        if(document.getElementById(username2ControlId) != null) {
             document.getElementById(username2ControlId).value = value;
        }
    }
}

function setAssetId(value) {
    if(assetControlId != "") {
         document.getElementById(assetControlId).value = value;
    }
}

function readCookie() {
//    var mainSiteCookie = "Slamball";
//	var mainSiteCookieEQ = mainSiteCookie + "=";
//	var ca = document.cookie.split(';');
//	var mainSiteCookieValue = "";
//	for(var i=0;i < ca.length;i++) {
//		var c = ca[i];
//		while (c.charAt(0)==' ') c = c.substring(1,c.length);
//		if (c.indexOf(mainSiteCookieEQ) == 0) {
//		    return c.substring(mainSiteCookieEQ.length,c.length);
//		}
//	}
	return "anonymus";
}


function updateCommentsButtonLink() {
    if(commentsButtonControlId != "" && document.getElementById(commentsButtonControlId) != null) {
        document.getElementById(commentsButtonControlId).href = "#comments";
    }
}

function renderCommentsForm() {
    var username = readCookie();
    if(document.getElementById("commentsForm") != null) {
        if(username != "") {
            document.getElementById("commentsForm").className = "commentsFormVisible";
        } else {
            document.getElementById("commentsForm").className = "commentsFormHidden";
        }
    }
}

function renderCommentsLinks() {
    var username = readCookie();
    if(document.getElementById("commentsLinks") != null) {
        if(username != "") {
            document.getElementById("commentsLinks").className = "commentsFormHidden";
        } else {
            document.getElementById("commentsLinks").className = "commentsFormVisible";
        }
    }
}

function renderHeaderLinks() {
//    var username = readCookie();
//    if(document.getElementById("hlUsername") != null) {
//        if(username != "") {
//            document.getElementById("spanWelcome").className = "commentsFormVisible";
//            document.getElementById("spanRegister").className = "commentsFormHidden";
//            document.getElementById("hlUsername").innerHTML = username;
//            document.getElementById("hlUsername").href += username;
//        } else {
//            document.getElementById("spanWelcome").className = "commentsFormHidden";
//            document.getElementById("spanRegister").className = "commentsFormVisible";
//        }
//    }
}

function clearCookie(){
    var delCookie = "Slamball=; expires=Thu, 01-Jan-70 00:00:01 GMT; ";
    document.cookie = delCookie;
    return true;
}

function setCookies(username){
    document.cookie = "Slamball=" + username + ";";
    return true;
}

function updateLoginStatus() {
    var username = readCookie();
    if(username != "") {
        renderHeaderLinks();
        renderCommentsLinks();
        renderCommentsForm();
        updateCommentsButtonLink();
    }
}

function autofitIframe(frame,name){
    if (element = document.getElementById(name)) {
        var mynode = element.contentWindow.document.body;
        element.height = mynode.scrollHeight + "px";
    }
}

if(typeof($) != 'undefined') {
$(function() {
    $('div.quote').cycle();
});
}

function WebForm_CallbackComplete_SyncFixed() {
  // SyncFix: the original version uses "i" as global thereby resulting in javascript errors when "i" is used elsewhere in consuming pages
  for (var i = 0; i < __pendingCallbacks.length; i++) {
   callbackObject = __pendingCallbacks[ i ];
  if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
   // the callback should be executed after releasing all resources
   // associated with this request.
   // Originally if the callback gets executed here and the callback
   // routine makes another ASP.NET ajax request then the pending slots and
   // pending callbacks array gets messed up since the slot is not released
   // before the next ASP.NET request comes.
   // FIX: This statement has been moved below
   // WebForm_ExecuteCallback(callbackObject);
   if (!__pendingCallbacks[ i ].async) {
     __synchronousCallBackIndex = -1;
   }
   __pendingCallbacks[i] = null;

   var callbackFrameID = "__CALLBACKFRAME" + i;
   var xmlRequestFrame = document.getElementById(callbackFrameID);
   if (xmlRequestFrame) {
     xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
   }

   // SyncFix: the following statement has been moved down from above;
   WebForm_ExecuteCallback(callbackObject);
  }
 }
}
window.onload = function Onloaad(){
if (typeof (WebForm_CallbackComplete) == "function") {
  // set the original version with fixed version
  WebForm_CallbackComplete = WebForm_CallbackComplete_SyncFixed;
}
}

