this.qsearch = this.reelzQSearch = new reelzQSearch();
function reelzQSearch() {
    var highlighted; var resultsOutput; var inputObject; var rowIndex = -1; var navigateObject = ''; var movieMineId = ''; var posObject; var listId; var listType;
    var movieFriends = 0; 
    function cancelBubbleFunction(e) {
        if (resultsOutput != null)
            resultsOutput.style.display = "none";
    }       
    function clearSearchFunction(element) {
        if (element.value == element.defaultValue)
            element.value = "";
    }
    function highlightFunction(element) {
        if (highlighted != null)
            highlighted.style.backgroundColor = "";           
        highlighted = element;
        element.style.backgroundColor = "#DDDDDD";
        //displayObjectInfo(element);
    }
    function displayObjectInfo(element) {
        var loadPage = '';
        if (element.getAttribute("objecttype") == 'Movie') {
            loadPage = '/services/movie/getmoviedetail.aspx?format=html&mobile=true&movieid=' + element.getAttribute("objectid");
        } else if (element.getAttribute("objecttype") == 'Person') {
            loadPage = '/services/person/getpersondetail.aspx?format=html&mobile=true&personid=' + element.getAttribute("objectid");
        }
        if (loadPage.length > 0) {
            if ($(element).parent().find('.qsearch-info').length <= 0) {
                $(element).parent().prepend('<span class="qsearch-info"></span>');
                $(element).parent().find('.qsearch-info').css('left', parseInt($(element).parent().innerWidth()) + 2 + 'px');
            }
            else {
                $(element).parent().parent().find('.qsearch-info').html('');
            }
            $(element).parent().parent().find('.qsearch-info').prepend('<span class="loading"></span>');
            $(element).parent().parent().find('.qsearch-info').load(loadPage, function() { });
        } 
    }
    function exploreObjectFunction(element) {
        resultsOutput.style.display = "none";
        exploreMoreFunction(element.getAttribute("objectid"), element.getAttribute("objecttype"), movieMineId);
    }
    function getClipsFunction(element) {
        resultsOutput.style.display = "none";
        var oid = element.getAttribute("objectid");
        var ot = element.getAttribute("objecttype");
        //alert(oid);
        //alert(ot);
        var index = location.href.indexOf('lt=', 0);
        if (index >= 0) {
            location.href = location.href.substring(0, index) + "lt=" + ot + "&oid=" + oid;
        } else {
            location.href = location.href + "?lt=" + ot + "oid=" + oid;
            return false;
        }
        return false;
    }
    function navigateToLinkFunction(element) {
        resultsOutput.style.display = "none";
        inputObject.value = element.getAttribute("title");
        self.location = element.getAttribute("link");
    }
    function exploreMoreFunction(exploreNodeValue, type, id) {
        var explorer = document.getElementById(id);
        if (explorer != null) {
            if (type == 'Movie') {
                explorer.setExplorerMovie(exploreNodeValue);
                if (typeof removeReleaseDateFocus != 'undefined')
                    removeReleaseDateFocus();
            }
            else if (type == 'Person') {
            explorer.setExplorerPerson(exploreNodeValue);
            if (typeof removeReleaseDateFocus != 'undefined')
                removeReleaseDateFocus();
            }
            else if (type == 'releasedate')
                explorer.exploreReleaseDate(exploreNodeValue);
        }
        return false;
    }
    function addToListFunction(element) {
        if (resultsOutput != null)
            resultsOutput.style.display = "none";
        var objecttype_id = ($(element).attr("objecttype") == "Movie") ? 8 : 10;
        var object_id = $(element).attr("objectid");
        var qsTxtBox = $(element).parent().parent().siblings();
        if (qsTxtBox != null) {
            $(qsTxtBox).attr('objecttype', objecttype_id);
            $(qsTxtBox).attr('objectid', object_id);
            $(qsTxtBox).attr('title', $(element).attr("title"));
            var objectYear = $(element).attr("objectyear");
            if (objectYear == undefined || objectYear == null) {
                $(qsTxtBox).removeAttr('objectyear');
            } else {
                $(qsTxtBox).attr('objectyear', objectYear);
            }
            var objectPhoto = $(element).attr("objectphoto");
            if (objectPhoto == undefined || objectPhoto == null) {
                $(qsTxtBox).removeAttr('objectphoto');
            } else {
                $(qsTxtBox).attr('objectphoto', objectPhoto);
            }
        }
        return false;
    }

    function populateSearchFieldFunction(e) {       
        try {
            switch (e.keyCode) {
                case 13:
                    {
                        if (navigateObject == "quicksearch" && (inputObject != null && inputObject.value != "" && rowIndex == -1) || e.type == "mousedown") {
                            searchFunction(e);
                        }
                        else {
                            if (resultsOutput != null && resultsOutput.childNodes != null && resultsOutput.childNodes.length > rowIndex) {
                                inputObject.value = resultsOutput.childNodes[rowIndex].getAttribute("title");
                                navigateFunction(resultsOutput.childNodes[rowIndex]);
                            }
                        }
                        break;
                    }
            case 38: {
                    if (resultsOutput != null && resultsOutput.childNodes != null && rowIndex > 0) {
                        rowIndex--;highlightFunction(resultsOutput.childNodes[rowIndex]);
                    }
                    break;
                }
                case 40: {
                    if (resultsOutput != null && resultsOutput.childNodes != null && rowIndex < resultsOutput.childNodes.length - 1) {
                        rowIndex++;highlightFunction(resultsOutput.childNodes[rowIndex]);
                    }
                    break;
                }
            }
        }
        catch(err) { }            
    }
    function refreshResultsFunction(obj, resultsOut, type, navigateFunctionName, id, e) {
        if (obj != null && obj.value != "") {
            //var resultObj = resultsOut;
            movieMineId = id;
            movieFriends = id;
            navigateObject = navigateFunctionName;
            if ($(obj).attr("listType")) {
                listType = $(obj).attr("listType");
                listId = $(obj).attr("listId");
                //resultObj = $("#" + listId).find("#quickSearchOutput");
            }
            if (e.type == "keyup") {
                if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13) {
                    resultsOutput = resultsOut;
                    inputObject = obj;
                    var loadPage = 'http://partner.reelzchannel.com/services/Search/QuickSearch.aspx?format=html&type=' + type + '&keyword=' + encodeURIComponent(obj.value);
                    $(resultsOutput).load(loadPage, showNewContent); 
                }
                else {
                    populateSearchFieldFunction(e);
                }
            }
            else {
                    resultsOutput = resultsOut;
                    inputObject = obj;
                    var loadPage = 'http://partner.reelzchannel.com/services/Search/QuickSearch.aspx?format=html&type=' + type + '&keyword=' + encodeURIComponent(obj.value);
                    $(resultsOutput).load(loadPage, showNewContent);
            }
        }
    }
    function showNewContent() { 
        if ($(resultsOutput) != null && $(resultsOutput).text() != "") {
            rowIndex = -1;
            //resultsOutput.style.top = (parseInt(inputObject.offsetHeight) + parseInt(resultsOutput.offsetHeight) + 1) + "px";
            resultsOutput.style.top = (parseInt(inputObject.offsetHeight)  + 1) + "px";
            resultsOutput.style.display = "inline";
        } else if ($(resultsOutput) != null) {
            resultsOutput.style.display = "none";
        }
    }
    
    function setPlayerClipList(element) {
        //document.getElementById("searchboxDiv").getElementsByTagName("input")[0].value = element.getAttribute("title");
        var objecttype_id = ($(element).attr("objecttype") == "Movie") ? 'm' : ($(element).attr("objecttype") == "Show") ? 's' : 'p';
        var object_id = $(element).attr("objectid");
        setListId(objecttype_id + object_id);
    }

    function setClipList(element) {
        var objecttype_id = ($(element).attr("objecttype") == "Movie") ? 'm' : ($(element).attr("objecttype") == "Show") ? 's' : 'p';
        var object_id = $(element).attr("objectid");
        var loadPage = 'http://partner.reelzchannel.com/services/clips/getclips.aspx?f=html&idl=' + objecttype_id + object_id + '&c=100&p=1';
        $('#cliplistText').load(loadPage, function() {
             $('ul.clipList li a').click(function(event) {
                event.preventDefault();
                var urlparts = $(this).attr('href').split('/');
                var movieid = urlparts[urlparts.length - 3];
                setMovieId(movieid);
                });   
        });
    }    
    
    function setStartOrEndNode(element) {
        inputObject.value = element.getAttribute("title");

        var objectId;
        var objectType;
        if (movieMineId == "start") {
            objectId = $('input.object-id-start');
            objectType = $('input.object-type-start');
        } else {
            objectId = $('input.object-id-end');
            objectType = $('input.object-type-end');
        }
        if (objectId != null) objectId.val(element.getAttribute("objectid"));
        if (objectType != null) objectType.val(element.getAttribute("objecttype"));
    }
    function navigateFunction(element) {
        inputObject.value = element.getAttribute("title");

        if (navigateObject == "setStartOrEndNode") {
            setStartOrEndNode(element);
            return;
        }

        if (navigateObject == "setPlayerClipList") {
            setPlayerClipList(element);
            return;
        }

        if (navigateObject == "setClipList") {
            setClipList(element);
            return;
        } 

        var objectId = $('input.object-id');
        if (objectId != null) objectId.val(element.getAttribute("objectid"));
        var objectType = $('input.object-type');
        if (objectType != null) objectType.val(element.getAttribute("objecttype"));

        if (navigateObject == "quicksearch") {
            quickSearchNavigateFunction(element);
        }
        else if (navigateObject == "addToList") {
            addToListFunction(element);
        }
        else if (navigateObject == "navigateToLink") {
            navigateToLinkFunction(element);
        }
        else if (navigateObject == "exploreObject") {
            exploreObjectFunction(element);
        }
        else if (navigateObject == "getClips") {
            getClipsFunction(element);
        } 
        else if (navigateObject == "getMovieFriends") {
            getMovieFriendsFunction(element);
        }
    }
    function getMovieFriendsFunction(element) {
        resultsOutput.style.display = "none";
        var url = "/movie-friends/" + movieFriends;
        url += "-" + element.getAttribute("objectid");
        self.location = url;
    }
    function quickSearchNavigateFunction(element) {
        resultsOutput.style.display = "none";
        inputObject.value = element.getAttribute("title");
        self.location = element.getAttribute("link");
        saveSearch();
    }
    function saveSearch() {
        if (inputObject != null) {
            xmlHttp = getXmlHttpObject();
            xmlHttp.open("GET", "/webservices/quicksearch.asmx/SaveSearch?keyword=" + inputObject.value, true);
            xmlHttp.send(null);
        }
    }
    function searchFunction(e) {
        if (inputObject != null && inputObject.value != "") {
            if (inputObject.value != inputObject.defaultValue) {
                document.location = "/search.aspx?keywords=" + encodeURIComponent(inputObject.value);
                saveSearch();
            }
            else {
                inputObject.focus();
            }
        }
        return false;
    }
    this.cancelBubble = cancelBubbleFunction;
    this.clearSearch = clearSearchFunction;
    this.highlight = highlightFunction;
    this.navigateToLink = navigateToLinkFunction;
    this.exploreObject = exploreObjectFunction;
    this.getClips = getClipsFunction;
    this.populateSearchField = populateSearchFieldFunction;
    this.refreshResults = refreshResultsFunction;
    this.navigate = navigateFunction;
    this.exploreMore = exploreMoreFunction;
    this.search = searchFunction;
}
