﻿
var isClicked = false;

document.onclick = function() {
    if (!isClicked) {
        closeAll();
    }

    isClicked = false;
}

document.ondoubleclick = function() {
    if (!isClicked) {
        closeAll();
    }

    isClicked = false;
}

//document.onmouseup = function ()
//{                                                                
//    sideTimer = setTimeout( closeAll, 1000);                                            
//}

function closeAll() {
    var list = [1, 5, 3, 4];

    // Check if what you want to see is finished loading
    if (document.getElementById("ssi3Input") == null)
        return;

    for (var i = 0; i < list.length; i++) 
    {
        var el = document.getElementById("ssi" + list[i]);
        if (el != null) {
            document.getElementById("ssi" + list[i]).style.display = 'none';
        }
    }

    if (document.getElementById("ssi3Input").value == "" &&
        typeof InputHeader2 != "undefined") {
        document.getElementById("ssi3Input").value = InputHeader2;
    }

    if (document.getElementById("ssi5Input").value == "" &&
        typeof InputHeader3 != "undefined") {
        document.getElementById("ssi5Input").value = InputHeader3;
    }

    selectedElementID = "";
}

function supportedClick() {
    isClicked = true;
}

function swapFocus(ElementName) {
    isClicked = true;
    //clearTimeout(sideTimer);

    var oElement = document.getElementById(ElementName);
    var isElementOpen = (oElement.style.display == 'block');

    if (isElementOpen) {
        closeAll();
    } else {
        setFocus(ElementName);
    }
}

function setFocus(ElementName) {
    isClicked = true;
    //clearTimeout(sideTimer);

    var oElement = document.getElementById(ElementName);
    var isElementOpen = (oElement.style.display == 'block');

    if (!isElementOpen) {
        closeAll();
        oElement.style.display = 'block';
        selectedElementID = ElementName;
    }
}

function buildControlList(array, controlPrefix, startWith) {
    var htmlStr = "";

    if (array == null) {
        return;
    }

    if (typeof (startWith) == 'undefined' || startWith == "") {
        var itemsCount = array.length;

        if (itemsCount > 50) {
            itemsCount = 50;
        }

        for (var i = 0; i < itemsCount; i++) {
            var value = array[i];
            htmlStr += "<li><a onclick=\"document.getElementById('" + controlPrefix + "Input').value=this.innerHTML;\" href=\"javascript:;\">" + value + "</a></li>";
        }
    } 
    else {
        var counter = 0;
        
        for (var i = 0; i < array.length; i++) {
            if (counter >= 50) {
                break;
            }

            var value = array[i];
            var firstLetter = startWith.substring(0,1);
            var firstLetterUpperCase = firstLetter.toUpperCase();
            
            if(startWith.length == 1)
            {
                if (value.indexOf(firstLetter) >= 0 || value.indexOf(firstLetterUpperCase) >= 0) {
                    htmlStr += "<li><a onclick=\"document.getElementById('" + controlPrefix + "Input').value=this.innerHTML;\" href=\"javascript:;\">" + value + "</a></li>";
                    counter++;
                }
            }
            else
            {
                var tempStartWith = startWith.substring(1, startWith.length);
            
                if (value.indexOf(firstLetter + tempStartWith) >= 0 || value.indexOf(firstLetterUpperCase + tempStartWith) >= 0) {
                    htmlStr += "<li><a onclick=\"document.getElementById('" + controlPrefix + "Input').value=this.innerHTML;\" href=\"javascript:;\">" + value + "</a></li>";
                    counter++;
                }
            }
        }
    }

    var oUL = document.getElementById(controlPrefix + "UL");
    oUL.innerHTML = htmlStr;
}

function focusOnActor(control) {
    if (selectedElementID != 'ssi3') {
        setFocus('ssi3', false);

        if (control.value == InputHeader2) {
            control.value = '';
        }
        
        buildControlList(ActorList, 'ssi3', control.value);
    } else {
        setFocus('ssi3', false);
    }
}

function focusOnDirector(control) {
    if (selectedElementID != 'ssi5') {
        setFocus('ssi5', false);

        if (control.value == InputHeader3) {
            control.value = '';
        }

        buildControlList(DirectorList, 'ssi5', control.value);
    } else {
        setFocus('ssi5', false);
    }
}

function SubmitSearch(isFriendlyUrlSupported) {
    var genre = document.getElementById("ssi1Input").value;
    //var mood = document.getElementById("ssi2Input").value;        
    var actor = document.getElementById("ssi3Input").value;
    var director = document.getElementById("ssi5Input").value;
    var orderBy = document.getElementById("ssi4Input").value;
    
    var queryStr = "";
    // friendlyUrl use / and regular use |
    var sSeperator = (isFriendlyUrlSupported=="True")? "/" : "|";
    
    if(isFriendlyUrlSupported == "False"){
        queryStr = "SearchType=ByMovieFinder";
        queryStr += "&TagPairs=";
    }
    
    //Genre
    if (typeof InputHeader1 != "undefined" && genre != InputHeader1){
        queryStr += InputSearchTag1 + "=" + escape(genre) + sSeperator;
    }
    else{
        genre = "";
    }
    
    //Mood
//    if (typeof moodHeader != "undefined" && mood != moodHeader)
//    {
//        queryStr += "&ByMood=" + escape(mood);
//    }
//    else
//    {
//        genre = "";
//    }
    
    //Actor
    if (typeof InputHeader2 != "undefined" && actor != InputHeader2) {
        //queryStr += "&ByActor=" + escape(actor);
        queryStr += InputSearchTag2 + "=" + actor + sSeperator;
    }
    else{
        actor = "";
    }
    
    //Director
    if (typeof InputHeader3 != "undefined" && director != InputHeader3) {
        queryStr += InputSearchTag3 + "=" + director + sSeperator;
    }
    else{
        director = "";
    }

    // Remove the last "|" from the stirng
    queryStr = queryStr.substring(0, queryStr.length -1);
    
    // in case of clicking search button without selecting from any dropdown
    if(queryStr=="") queryStr = "TagPairs"
    
    //Order By
    if (typeof orderBy != "undefined") {
        queryStr += (isFriendlyUrlSupported=="True"? "/" : "&") + "OrderBy=" + escape(orderBy);
    }                
    else{
        orderBy = "";
    }
    
    if(isFriendlyUrlSupported=="True")
    {
        window.location.href = applicationBaseURL + "search/" + queryStr;
    } 
    else 
    {
        window.location.href = applicationBaseURL + "search.aspx?" + queryStr;
    }
}

