function search()
{
    el('search').submit();
}

function getEvent(event)
{
    if (!event)
    {
        event = window.event;
    }
    return event;
}

var toggledStates = new Array();
var collapsed = 0;
var expanded = 1;
function toggle(cat)
{
    //Base State is collapsed.
    //if no entry in the toggledStates array assume current state is collapsed
    //and expand the cat.
    var ec = toggledStates[cat];
    if (isUndefined(ec) || (ec == collapsed))
    {
        //Change the image to expanded
        
        if (el('ecimg_' + cat) != null)
        {
            el('ecimg_' + cat).src = '/images/expanded.gif';
            toggledStates[cat] = expanded;
            //Show the values
            showblock('catvalues_' + cat);
        }
    }
    else
    {
        //Change the image to collapsed
        if (el('ecimg_' + cat) != null)
        {
            el('ecimg_' + cat).src = '/images/collapsed.gif';
            toggledStates[cat] = collapsed;
            //Hide the values
            hide('catvalues_' + cat);
        }
    }
    
}

function refine(cat, value)
{
    el('refineCat').value = cat;
    el('refineValue').value = value;
    el('refineSearchForm').submit();
    
}

function highlightRefineValue(cat, value)
{
    var refineValueList = el('catvalues_' + cat);
    if (refineValueList == null)
    {
        return;
    }
    var refineListElements = refineValueList.childNodes;
    for (var i = 0; i < refineListElements.length; i++)
    {            
        var oneRefineListElement = refineListElements[i];        
        var oneRefineListElementValues = oneRefineListElement.childNodes;
        for (var j = 0; j < oneRefineListElementValues.length; j++)
        {
            if (oneRefineListElementValues[j].style)
            {
                oneRefineListElementValues[j].style.fontSize = '10px';
                oneRefineListElementValues[j].style.fontWeight = 'bold';
                oneRefineListElementValues[j].style.textDecoration = 'underline';
                oneRefineListElementValues[j].style.backgroundColor = "transparent";
                oneRefineListElementValues[j].style.color = "#75AF71";
                oneRefineListElementValues[j].style.padding = "0px"
                oneRefineListElementValues[j].style.lineHeight = "1em"
            }
        }
    }
}

function removeFilter(cat, value)
{
    el('removeCat').value = cat;
    el('removeValue').value = value;
    el('removeFilterForm').submit();
}

function searchSuggested(query)
{
    el('search_text_input').value = query;
    el('search').submit();
}

function searchPrevious(query)
{
    el('search_text_input').value = query;
    el('search').submit();
}

function saveProfile(id)
{
    show('progress');
    el('progress').style.left = document.body.clientWidth - el('progress').clientWidth;
    el('progress').style.top = document.body.scrollTop;  
    var transaction = YAHOO.util.Connect.asyncRequest(
                                 'POST',
                                 saveProfileUrl + '/' + 'operation-saveProfile' + '/' + 'id-' + id,
                                 personalizationCallback,
                                 "");
}

function deleteProfile(id)
{
    el('id').value = id;
    el('deleteProfileForm').submit();
}
var tid;
function timeoutFn()
{
    window.clearTimeout(tid);
    hide('progress');
    
}
var personalizationCallback = 
{
   success : function(o)
   {
       //alert(o.responseText);
       //var response = eval('(' + o.responseText + ')');
       tid = window.setTimeout(timeoutFn, 1000);       
   },
   failure : function(o)
   {
       //alert(o.responseText);
       //var response = eval('(' + o.responseText + ')');
       hide('progress');
   }
};





function getRefineChoices(id)
{
    var transaction = YAHOO.util.Connect.asyncRequest(
                                 'POST',
                                 refineMoreUrl,
                                 refineMoreCallback,
                                 "refineChoices=true&" + 
                                 "id=" + id);
}

function showRefineMoreSimplified(event, id) {
    showblock('hiddenBlock_' +id);
    hide('refineMoreLink_' +id);
}
function showRefineMore(event, id)
{
    event = getEvent(event); 
    var left = 0;
    var top = 0;
    var obj = null;
    if (event.srcElement)
    {
        obj = event.srcElement;
    }
    else
    if (event.target)
    {
        obj = event.target;
    }
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            left += obj.offsetLeft;
            top += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    el('refineTree').style.position = 'relative';
    clickX = event.clientX;
    clickY = event.clientY;
    
    el('refineMoreBody').innerHTML = "<span class='loading'>Loading...</span>";
    show('refineMore');
    getRefineChoices(id);
    el('overlayRefineMoreLink').style.left = left;
    el('overlayRefineMoreLink').style.top = top;
    show('overlayRefineMoreLink');
    el('refineMore').style.left = el('overlayRefineMoreLink').offsetLeft +  el('overlayRefineMoreLink').offsetWidth - 2;
    el('refineMore').style.top = el('overlayRefineMoreLink').offsetTop - (el('refineMore').offsetHeight/2) + (el('overlayRefineMoreLink').offsetHeight/2);//el('refineMore').offsetTop - 30;
    el('refineMore').style.zIndex = 1;
    el('refineMore').style.position = 'absolute';
    el('refineMore').style.border = '2px solid #1C457C';
    
    if (event.stopPropagation)
    {
        event.stopPropagation();
    }
    else
    {
        window.event.cancelBubble = true;
    }
}

var refineMoreCallback = 
{
   success : function(o)
   {
       //alert(o.responseText);
       el('refineMoreBody').innerHTML = o.responseText;
       //show('refineMoreBody');
       //ag_scrollIt();
       
       //var response = eval('(' + o.responseText + ')');
       //tid = window.setTimeout(timeoutFn, 1000);       
   },
   failure : function(o)
   {
       //alert(o.responseText);
       //var response = eval('(' + o.responseText + ')');
       //hide('progress');
   }
};

function closeChoices()
{
    hide('overlayRefineMoreLink');
    hide('refineMore');
    
}

var cnt = 0;
function closeRefineBox()
{
   cnt = 0;
   //tid = window.setTimeout(animateRefineBoxClose, 20);       
   hide('refineMore');
}

function animateRefineBoxClose()
{
    var id = 'refineMore';
    //alert("here " + el('refineMore').offsetWidth);
    el('refineMoreBody').style.width = (el('refineMoreBody').offsetWidth - 10) + 'px';
    el(id).style.width = (el(id).offsetWidth - 10) + 'px';
    
    el('refineMoreBody').style.height = (el('refineMoreBody').offsetHeight - 10) + 'px';
    el(id).style.height = (el(id).offsetHeight - 10) + 'px';
    //alert("here " + el(id).offsetWidth);
    //window.clearTimeout(tid );
    //el(id).width = el(id).offsetWidth - 10;
    //alert(el(id).offsetWidth);
    if (el(id).offsetWidth <= 0 || el(id).offsetHeight <= 0)
    {
        //alert(el(id).offsetWidth);
        window.clearTimeout(tid);
        hide(id);
        //el(id).style.width = '400px';
    }
    else
    {
        cnt++;
        tid = window.setTimeout(animateRefineBoxClose, 20);
    }
}

function showLongDescription(docId)
{
    show('longdesc_' + docId);
    hide('shortdesc_' + docId);
}

var viewProfileDialog = null;
function showLongDescription_t(docId)
{
    if (viewProfileDialog == null) {
        viewProfileDialog = new YAHOO.widget.Dialog("profileDlg", {width:500,height:600,modal:true,fixedcenter:true,draggable:false,zIndex:100000});
    }
    YAHOO.util.Dom.get('viewProfileResult').innerHTML = '';
    YAHOO.util.Dom.removeClass('profileDlg','hide');
    viewProfileDialog.render();
    viewProfileDialog.show();
    YAHOO.util.Dom.removeClass('wait','hide');
    var transaction = YAHOO.util.Connect.asyncRequest(
                                 'POST',
                                 viewProfileUrl,
                                 viewProfileCallback,
                                 "id=" + docId + "&mode=bare");
}

var viewProfileCallback = 
{
   success : function(o)
   {
       YAHOO.util.Dom.get('viewProfileResult').innerHTML = o.responseText;       
       YAHOO.util.Dom.addClass('wait','hide');
              
   },
   failure : function(o)
   {
       viewProfileDialog.close();
   }
};

function showShortDescription(docId)
{
    show('shortdesc_' + docId);
    hide('longdesc_' + docId);
}

function captureKeyPressFreeformRefine(evt)
{
    var code = -1;
    evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) 
    {
        code = (evt.keyCode) ? evt.keyCode : evt.which
    }
    if (code == 13)
    {
        refine('REFINE_FREEFORM_CATEGORY', el('refinKeyword').value);
    }
}    


function share(docId)
{
    var win = window.open("", "EmailWindow", "directories=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,height=500,width=460");
    document.email.target = "EmailWindow";
    document.email.documentId.value = docId;
    document.email.submit();    
}


function sendMail()
{
    document.sendEmailForm.submit();    
}

function captureLogin(evt)
{
	var code = -1;
    evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) 
    {
        code = (evt.keyCode) ? evt.keyCode : evt.which
    }
    if (code == 13)
    {
        login();
    }
}

function login()
{
    document.f.submit();
}

function registerUser()
{
    document.register.submit();
}

var clickX;
var clickY;
function showHelp(event, id)
{
    helpEvent = getEvent(event); 
    clickX = helpEvent.clientX;
	clickY = helpEvent.clientY;         
    var transaction = YAHOO.util.Connect.asyncRequest(
                                 'POST',
                                 helpUrl,
                                 showHelpCallback,                                 
                                 "id=" + id);
}

var showHelpCallback = 
{
   success : function(o)
   {
       var helpDiv = el('help');
       if (helpDiv == null)
       {
   	       helpDiv = document.createElement('div');
   	       helpDiv.id= 'help';   	       
   	       document.body.appendChild(helpDiv);
   	   }
   	   helpDiv.innerHTML = o.responseText;
   	   show('help');
   	   positionPopup();
       //alert(o.responseText);
       //var response = eval('(' + o.responseText + ')');
       
   },
   failure : function(o)
   {
       //alert(o.responseText);
       //var response = eval('(' + o.responseText + ')');
       
   }
}


function positionPopup()
{
   var popupWidth = el('help').offsetWidth;
   var popupHeight = el('help').offsetHeight;
   var windowWidth = document.body.offsetWidth;
   var windowHeight = document.body.offsetHeight;
   var scrollTop = document.body.scrollTop;
   var scrollLeft = document.body.scrollLeft;   
   //Check if the popup is forcing a scroll right
   //If it is try to position it to the left.
   //But if the left is clipping the window positiong to the right anyways   
   var overflowsRight = (clickX + popupWidth) > windowWidth;
   var posnLeft = clickX + scrollLeft;
   if (overflowsRight){posnLeft = posnLeft - popupWidth;}
   if (posnLeft < 0)
   {
       posnLeft = clickX + scrollLeft;
   }
   //Check if the popup is forcing a scroll down
   //If it is try to position it to the top.
   //But if the left is clipping the window positiong to the right anyways
   var overflowsBottom = (clickY + popupHeight) > windowHeight;
   var posnTop = clickY + scrollTop;
   if (overflowsBottom){posnTop = posnTop - popupHeight;}
   if (posnTop < 0)
   {
       var posnTop = clickY + scrollTop;
   }           
   //
   el('help').style.left = posnLeft;
   el('help').style.top = posnTop;
}


var lastLoginDocId = null;
function getLastLoginDate(docId, websiteUrl, objectUrl) {
    //alert("this functionality has been temporarily disabled");
    //return;
    lastLoginDocId = docId;
    var el = YAHOO.util.Dom.get("last_login_wait_indicator_" + lastLoginDocId);			
	YAHOO.util.Dom.removeClass(el, "hide");
    var transaction = YAHOO.util.Connect.asyncRequest(
                                 'POST',
                                 lastLoginUrl,	
                                 lastLoginCallback,
                                 "objectTypeName=" + "Profile" + "&" + 
                                 "websiteUrl=" + websiteUrl + "&" + 
                                 "objectUrl=" + objectUrl + "&" + 
                                 "fieldName=" + "lastOnline");

}

var lastLoginCallback = 
{
   success : function(o)
   {       
       var msg = o.responseText;
       if (msg == null || msg == '' || msg == 'null'){return lastLoginCallback.failure(o);}
       var el = YAHOO.util.Dom.get("last_login_wait_indicator_" + lastLoginDocId);			
	   YAHOO.util.Dom.addClass(el, "hide");
	   el = YAHOO.util.Dom.get("last_login_msg_" + lastLoginDocId);
	   YAHOO.util.Dom.addClass(el, "success");
	   el.innerHTML = o.responseText;
	   lastLoginDocId = null;       
   },
   failure : function(o)
   {
       var el = YAHOO.util.Dom.get("last_login_wait_indicator_" + lastLoginDocId);			
	   YAHOO.util.Dom.addClass(el, "hide");
	   el = YAHOO.util.Dom.get("last_login_msg_" + lastLoginDocId);
	   YAHOO.util.Dom.addClass(el, "error");
	   el.innerHTML = 'error';
	   lastLoginDocId = null;       
   }
}

var currentReviewDocId = null;
function getReviews(id, author, title)
{
    currentReviewDocId = id;
    var wait = YAHOO.util.Dom.get("get_reviews_wait_indicator_" + currentReviewDocId);			
	YAHOO.util.Dom.removeClass(wait, "hide");
    el('reviewButton' + id).disabled = true;
    var url = reviewsUrl;
    var transaction = YAHOO.util.Connect.asyncRequest(
                                 'POST',
                                 url,
                                 reviewsCallback,
                                 "title=" + title + "&author=" + author); 
}

var reviewsCallback = 
{
   success : function(o)
   {
       if (currentReviewDocId == null) {
           return;
       }
       var reviewContainerName = "reviews" + currentReviewDocId;
       el("reviews_internal" + currentReviewDocId).innerHTML = o.responseText;
       el('reviewButton' + currentReviewDocId).disabled = false;
       var wait = YAHOO.util.Dom.get("get_reviews_wait_indicator_" + currentReviewDocId);			
	   YAHOO.util.Dom.addClass(wait, "hide");
	   currentReviewDocId = null;       
	   YAHOO.util.Dom.removeClass(reviewContainerName,'hide');
	   var attributes = {height: { to: 280 }};
	   var anim = new YAHOO.util.Anim(reviewContainerName, attributes);
	   //anim.onComplete.subscribe(reviewsClosed);
	   anim.animate();       
	   
   },
   failure : function(o)
   {
       //alert(o.responseText);
       //var response = eval('(' + o.responseText + ')');
       //hide('progress');
       el('reviewButton' + currentReviewDocId).disabled = false;       
       var wait = YAHOO.util.Dom.get("get_reviews_wait_indicator_" + currentReviewDocId);			
	   YAHOO.util.Dom.addClass(wait, "hide");
	   currentReviewDocId = null;
   }
};

function reviewsClosed() {
    //YAHOO.util.Dom.addClass(reviewContainerName,'hide');
}

function closeReviews(id) {
    var reviewContainerName = "reviews" + id;
    var attributes = {height: { to: 1 }};
    var anim = new YAHOO.util.Anim(reviewContainerName, attributes);
    anim.onComplete.subscribe(reviewsClosed);
    anim.animate();    
    //YAHOO.util.Dom.addClass(reviewContainerName,'hide');
    
}

function hideLastCreatedLoad() {
    hide('loading');
}
