var DynamicSearchForm = {
    ddlMyGender: null, 
    ddlSeekingGender: null, 
    ddlAgeRanges: null, 
    txtPostalCode: null, 
    postalCodeLabel: "", 
    updateSearchTimer: null, 
    currentCountry: "", 
    currentCityId: -1, 
    groupKey: "", 
    
    FormUpdated: function(updatedElem) { 
        clearTimeout(DynamicSearchForm.updateSearchTimer);
        DynamicSearchForm.updateSearchTimer = setTimeout(DynamicSearchResults.DoSearch, 750);
    }, 
    
    FocusPostalCode: function() { 
        if (DynamicSearchForm.txtPostalCode == null || DynamicSearchForm.txtPostalCode == undefined) return;
        if (DynamicSearchForm.txtPostalCode.value == DynamicSearchForm.postalCodeLabel) { 
            DynamicSearchForm.txtPostalCode.value = "";
            DynamicSearchForm.txtPostalCode.style.color = "#000000";
        }
        else DynamicSearchForm.txtPostalCode.select();
    }, 
    
    BlurPostalCode: function() { 
        if (DynamicSearchForm.txtPostalCode == null || DynamicSearchForm.txtPostalCode == undefined) return;
        if (DynamicSearchForm.txtPostalCode.value == null || DynamicSearchForm.txtPostalCode.value == "" 
            || DynamicSearchForm.txtPostalCode.value == DynamicSearchForm.postalCodeLabel) { 
            DynamicSearchForm.txtPostalCode.value = DynamicSearchForm.postalCodeLabel;
            DynamicSearchForm.txtPostalCode.style.color = "#A3A3A3";
        }
    }
};

var DynamicSearchResults = { 
    myGender: 1, 
    wdGender: 2, 
    ageRange: "18-45", 
    postalCode: "", 
    usePostalCode: null, 
    loadingImg: null, 
    loadingImgElem: null, 
    searchLoadingElem: null, 
    searchPhotosElem: null, 
    photos: null, 
    photosLoaded: 0, 
    animationInterval: null, 
    photoContainer1: null, 
    photoContainer2: null, 
    frameDelay: 50, 
    frameDelta: 1, 
    c1Left: 0, 
    c2Left: 0, 
    latestSearch: 1, 
    profileDetailsElem: null, 
    isLoading: true, 
    hideDetailsTimer: null, 
    detailsPostAction: null, 
    
    DoSearch: function() { 
        DynamicSearchResults.HideProfileDetails();
    
        if (DynamicSearchResults.usePostalCode == null) DynamicSearchResults.usePostalCode = DynamicSearchForm.txtPostalCode != null && DynamicSearchForm.txtPostalCode != undefined;
        if (DynamicSearchResults.loadingImg == null) { 
            DynamicSearchResults.loadingImg = new Image();
            DynamicSearchResults.loadingImg.src = "/images/layout2008/home/updating_matches.gif";
            DynamicSearchResults.loadingImgElem = document.createElement("div");
            DynamicSearchResults.loadingImgElem.style.background = "transparent url(" + DynamicSearchResults.loadingImg.src + ") no-repeat scroll center center";
            DynamicSearchResults.loadingImgElem.style.width = "100%";
            DynamicSearchResults.loadingImgElem.style.height = "140px";
            DynamicSearchResults.loadingImgElem.style.position = "absolute";
            DynamicSearchResults.loadingImgElem.style.top = "0";
            DynamicSearchResults.loadingImgElem.style.left = "0";
            DynamicSearchResults.loadingImgElem.style.zIndex = "20";
            DynamicSearchResults.id = "DynamicSearchResultsLoading";
            
            DynamicSearchResults.searchLoadingElem = Get("SearchResultsLoading");
            DynamicSearchResults.searchPhotosElem = Get("SearchResultsPhotos");
        }
    
        DynamicSearchResults.myGender = DynamicSearchForm.ddlMyGender.options[DynamicSearchForm.ddlMyGender.selectedIndex].value;
        DynamicSearchResults.wdGender = DynamicSearchForm.ddlSeekingGender.options[DynamicSearchForm.ddlSeekingGender.selectedIndex].value;
        DynamicSearchResults.ageRange = DynamicSearchForm.ddlAgeRanges.options[DynamicSearchForm.ddlAgeRanges.selectedIndex].value;
        if (DynamicSearchResults.usePostalCode) { 
            if (DynamicSearchForm.txtPostalCode.value == DynamicSearchForm.postalCodeLabel) { 
                DynamicSearchResults.postalCode = "";
            }
            else if (DynamicSearchResults.postalCode != DynamicSearchForm.txtPostalCode.value) { 
                HomepageBranding.CheckNewZip(DynamicSearchForm.txtPostalCode.value, DynamicSearchResults.postalCode, DynamicSearchForm.currentCityId, DynamicSearchForm.currentCountry);
                DynamicSearchResults.postalCode = DynamicSearchForm.txtPostalCode.value;
            }
        }
        
        CreateCookie("mygender", DynamicSearchResults.myGender, 365);
        CreateCookie("wdgender", DynamicSearchResults.wdGender, 365);
        CreateCookie("minage", DynamicSearchResults.ageRange.split("-")[0], 365);
        CreateCookie("maxage", DynamicSearchResults.ageRange.split("-")[1], 365);
        CreateCookie("zip", DynamicSearchResults.postalCode, 365);
        
        DynamicSearchResults.StopAnimation();
        DynamicSearchResults.searchLoadingElem.appendChild(DynamicSearchResults.loadingImgElem);
        DynamicSearchResults.isLoading = true;
        GetAjaxContent("/ajax/searchprofiles.ashx?r=10&co=" + DynamicSearchForm.currentCountry + "&cy=" + DynamicSearchForm.currentCityId + "&p=" + DynamicSearchResults.postalCode + "&g1=" + DynamicSearchResults.myGender + "&g2=" + DynamicSearchResults.wdGender + "&a1=" + DynamicSearchResults.ageRange.split("-")[0] + "&a2=" + DynamicSearchResults.ageRange.split("-")[1] + "&g=" + DynamicSearchForm.groupKey, function(searchID) { return function(response) { DynamicSearchResults.LoadSearchResults(response, searchID); } }(++DynamicSearchResults.latestSearch));
    }, 
    
    LoadSearchResults: function(response, searchID) { 
        if (response == null || response == undefined || response == "" || response == "ERROR" || searchID != DynamicSearchResults.latestSearch) { 
            ClearChildNodes(DynamicSearchResults.searchLoadingElem);
            DynamicSearchResults.isLoading = false;
            return;
        }
        
        var searchResultsData = response.split("[break]");
        var searchResults = new Array();
        DynamicSearchResults.photos = new Array();
        DynamicSearchResults.photosLoaded = 0;
        
        for (var r=0, result; result = searchResultsData[r]; r++) { 
            searchResults[r] = new SearchResult(result);
            DynamicSearchResults.photos[r] = new Image();
        }
        for (var r=0, result; result = searchResults[r]; r++) { 
            DynamicSearchResults.photos[r].onload = function() { DynamicSearchResults.photosLoaded++; DynamicSearchResults.DisplaySearchResults(searchResults); };
            DynamicSearchResults.photos[r].src = result.photoUrl;
        }
    }, 
    
    DisplaySearchResults: function(searchResults) { 
        if (DynamicSearchResults.photosLoaded < DynamicSearchResults.photos.length) return;
        
        ClearChildNodes(DynamicSearchResults.searchLoadingElem);
        DynamicSearchResults.isLoading = false;
        DynamicSearchResults.photosLoaded = 0;
        ClearChildNodes(DynamicSearchResults.searchPhotosElem);
        
        DynamicSearchResults.photoContainer1 = DynamicSearchResults.MakeResultsContainer(searchResults);
        DynamicSearchResults.photoContainer2 = DynamicSearchResults.MakeResultsContainer(searchResults);
        DynamicSearchResults.photoContainer1.style.left = "1220px";
        DynamicSearchResults.photoContainer2.style.left = "0px";
        DynamicSearchResults.c1Left = 1220;
        DynamicSearchResults.c2Left = 0;
        
        DynamicSearchResults.searchPhotosElem.appendChild(DynamicSearchResults.photoContainer1);
        DynamicSearchResults.searchPhotosElem.appendChild(DynamicSearchResults.photoContainer2);
        DynamicSearchResults.StartAnimation();
    }, 
    
    MakeResultsContainer: function(searchResults) { 
        var container = document.createElement("div");
        container.style.position = "absolute";
        container.style.zIndex = 9;
        container.style.top = "0px";
        for (var p=0, photo; photo = DynamicSearchResults.photos[p]; p++) { 
            var imgElem = document.createElement("img");
            var searchResult = searchResults[p];
            imgElem.width = 120;
            imgElem.height = 140;
            imgElem.style.paddingRight = "2px";
            imgElem.src = photo.src;
            imgElem.onmouseover = function(reqElem, result) { return function() { DynamicSearchResults.ShowProfileDetails(reqElem, result); DynamicSearchResults.StopAnimation(); }; }(imgElem, searchResult);
            imgElem.onmouseout = function() { DynamicSearchResults.hideDetailsTimer = setTimeout(function() { DynamicSearchResults.HideProfileDetails(); }, 300); };
            container.appendChild(imgElem);
        }
        return container;
    }, 
    
    StartAnimation: function() { DynamicSearchResults.StopAnimation(); DynamicSearchResults.animationInterval = setInterval(function() { DynamicSearchResults.DoAnimationFrame(); }, DynamicSearchResults.frameDelay); }, 
    StopAnimation: function() { clearInterval(DynamicSearchResults.animationInterval); },     
    DoAnimationFrame: function() { 
        if (DynamicSearchResults.c1Left >= 2440) { 
            DynamicSearchResults.c1Left = DynamicSearchResults.c2Left;
            DynamicSearchResults.c2Left = 0;
            
            var temp = DynamicSearchResults.photoContainer1;
            DynamicSearchResults.photoContainer1 = DynamicSearchResults.photoContainer2;
            DynamicSearchResults.photoContainer2 = temp;
            temp = null;
        };
        
        DynamicSearchResults.c1Left += DynamicSearchResults.frameDelta;
        DynamicSearchResults.c2Left += DynamicSearchResults.frameDelta;
        DynamicSearchResults.photoContainer1.style.left = (DynamicSearchResults.c1Left) + "px";
        DynamicSearchResults.photoContainer2.style.left = (DynamicSearchResults.c2Left) + "px";
    }, 
    
    ShowProfileDetails: function(requestElem, searchResult) { 
        if (DynamicSearchResults.isLoading) return;
        DynamicSearchResults.HideProfileDetails();
        clearTimeout(DynamicSearchResults.hideDetailsTimer);
        
        var dropbox = Get("EntityDropbox");
        if (dropbox == null || dropbox == undefined) return;
        DynamicSearchResults.profileDetailsElem = document.createElement("div");
        DynamicSearchResults.profileDetailsElem.className = "MemberIDCard";
        DynamicSearchResults.profileDetailsElem.style.top = (findPosY(requestElem) - 21) + "px";
        DynamicSearchResults.profileDetailsElem.style.left = (findPosX(requestElem) - 21) + "px";
        DynamicSearchResults.profileDetailsElem.onmouseover = function() { clearTimeout(DynamicSearchResults.hideDetailsTimer); };
        DynamicSearchResults.profileDetailsElem.onmouseout = function() { DynamicSearchResults.hideDetailsTimer = setTimeout(function() { DynamicSearchResults.HideProfileDetails(); }, 300); };
        
        var imgElem = document.createElement("img");
        imgElem.width = 120;
        imgElem.height = 140;
        imgElem.src = searchResult.photoUrl;
        imgElem.style.position = "absolute";
        imgElem.style.top = "21px";
        imgElem.style.left = "21px";
        
        var title = document.createElement("div");
        title.style.position = "absolute";
        title.style.top = "21px";
        title.style.left = "151px";
        title.style.width = "153px";
        title.style.fontSize = (searchResult.userName.length >= 18) ? "11px" : "14px";
        title.style.fontWeight = "bold";
        title.style.color = "#FFD312";
        title.appendChild(document.createTextNode(searchResult.userName));
        
        var details = document.createElement("div");
        details.style.position = "absolute";
        details.style.top = "41px";
        details.style.left = "151px";
        details.style.width = "153px";
        details.style.fontSize = "11px";
        details.style.color = "#FFFFFF";
        
        var line1 = searchResult.gender + ((searchResult.gender.length > 0) ? " / " : "") + searchResult.sexuality;
        var line2 = searchResult.race + ((searchResult.race.length > 0) ? " / " : "") + searchResult.religion;
        var line3 = searchResult.smoker + ((searchResult.smoker.length > 0) ? " / " : "") + searchResult.hasChildren;
        var line4 = searchResult.age;
        
        details.appendChild(document.createTextNode(line1));
        if (line1.length > 0) details.appendChild(document.createElement("br"));
        details.appendChild(document.createTextNode(line2));
        if (line2.length > 0) details.appendChild(document.createElement("br"));
        details.appendChild(document.createTextNode(line3));
        if (line3.length > 0) details.appendChild(document.createElement("br"));
        details.appendChild(document.createTextNode(line4));
        
        var button = document.createElement("a");
        button.className = "SignupButton";
        button.style.position = "absolute";
        button.style.top = "118px";
        button.style.left = "151px";
        button.style.paddingTop = "5px";
        button.href = "/join/";
        button.onclick = function() { eval(DynamicSearchResults.detailsPostAction); return false; };
        button.appendChild(document.createTextNode("Join for FREE to"));
        button.appendChild(document.createElement("br"));
        button.appendChild(document.createTextNode("View Full Profile"));
        
        DynamicSearchResults.profileDetailsElem.appendChild(imgElem);
        DynamicSearchResults.profileDetailsElem.appendChild(title);
        DynamicSearchResults.profileDetailsElem.appendChild(details);
        DynamicSearchResults.profileDetailsElem.appendChild(button);
        
        dropbox.appendChild(DynamicSearchResults.profileDetailsElem);
        dropbox = null;
        
        Get("favuser").value = searchResult.userID;
    }, 
    
    HideProfileDetails: function() { 
        if (DynamicSearchResults.profileDetailsElem == null || DynamicSearchResults.profileDetailsElem == undefined) return;
        var dropbox = Get("EntityDropbox");
        if (dropbox == null || dropbox == undefined) return;
        dropbox.removeChild(DynamicSearchResults.profileDetailsElem);
        dropbox == null;
        DynamicSearchResults.profileDetailsElem = null;
        DynamicSearchResults.StartAnimation();
        
        Get("favuser").value = -1;
    } 
};

SearchResult.prototype.userID;
SearchResult.prototype.userName;
SearchResult.prototype.gender;
SearchResult.prototype.sexuality;
SearchResult.prototype.race;
SearchResult.prototype.religion;
SearchResult.prototype.age;
SearchResult.prototype.smoker;
SearchResult.prototype.hasChildren;
SearchResult.prototype.photoUrl;

function SearchResult(dataString) { 
    var dataParts = dataString.split("[split]");
    this.userID = dataParts[0];
    this.userName = dataParts[1];
    this.gender = dataParts[2];
    this.sexuality = dataParts[3];
    this.race = dataParts[4];
    this.religion = dataParts[5];
    this.age = dataParts[6];
    this.smoker = dataParts[7];
    this.hasChildren = dataParts[8];
    this.photoUrl = dataParts[9];
}