function handleCategorySelect(id) {
    dataProvider.getList(id, null, null);
}
function init() {
    if (preSelectedCategory) {
        if (preSelectedVendor) {
            dataProvider.getListAtVendor(preSelectedCategory, preSelectedVendor);
        } else {
            handleCategorySelect(preSelectedCategory);
        }
    } else {
        handleCategorySelect(document.getElementById("vendorCategories").options[document.getElementById("vendorCategories").selectedIndex].value);
    }
	
    loginRequiredWin = new Windows("loginRequired", 0, 0);
    /*
    globalEventListener.addFunctionAtEvent("document", "mousemove", function(e) {
        loginRequiredWin.mouseMove(e);
    });
    globalEventListener.addFunctionAtEvent("loginRequiredWin.objectTitlebar", "mousedown", function(e) {
        loginRequiredWin.mouseDown(e);
    });
    globalEventListener.addFunctionAtEvent("loginRequiredWin.objectTitlebar", "mouseup", function(e) {
        loginRequiredWin.mouseDown(e);
    });
	*/
    
    shareWin = new Windows("share", 0, 0);
    /*
    globalEventListener.addFunctionAtEvent("document", "mousemove", function(e) {
        shareWin.mouseMove(e);
    });
    globalEventListener.addFunctionAtEvent("shareWin.objectTitlebar", "mousedown", function(e) {
        shareWin.mouseDown(e);
    });
    globalEventListener.addFunctionAtEvent("shareWin.objectTitlebar", "mouseup", function(e) {
        shareWin.mouseDown(e);
    });
    */
}

function shortInit() {
    loginRequiredWin = new Windows("loginRequired", 0, 0);
    /*
    globalEventListener.addFunctionAtEvent("document", "mousemove", function(e) {
        loginRequiredWin.mouseMove(e);
    });
    globalEventListener.addFunctionAtEvent("loginRequiredWin.objectTitlebar", "mousedown", function(e) {
        loginRequiredWin.mouseDown(e);
    });
    globalEventListener.addFunctionAtEvent("loginRequiredWin.objectTitlebar", "mouseup", function(e) {
        loginRequiredWin.mouseDown(e);
    });
    */
    
    shareWin = new Windows("share", 0, 0);
    /*
    globalEventListener.addFunctionAtEvent("document", "mousemove", function(e) {
        shareWin.mouseMove(e);
    });
    globalEventListener.addFunctionAtEvent("shareWin.objectTitlebar", "mousedown", function(e) {
        shareWin.mouseDown(e);
    });
    globalEventListener.addFunctionAtEvent("shareWin.objectTitlebar", "mouseup", function(e) {
        shareWin.mouseDown(e);
    });
    */
}

function selectVendor(index, id) {
    for (var i = 0; i < 14; i++) {
        if (document.getElementById("vendor_border_" + i)) {
            document.getElementById("vendor_border_" + i).style.visibility = "hidden";
        }
    }
    document.getElementById("vendor_border_" + index).style.visibility = "visible";
    
    dataProvider.getVendor(id);
}
function gotoPage(page) {
    dataProvider.getList(null, null, page);
}
function gotoVideoPage(page) {
    dataProvider.populateVendorVideosPane(page);
}
function gotoPicPage(page) {
    dataProvider.populateVendorPicsPane(page);
}
function handleSearchButtonClick() {
    dataProvider.getList(null, document.getElementById("searchText").value, null);
}
function changeTab(activeId) {
    if (activeId == "tabCompany") {
        document.getElementById("tabCompany").style.background = "url(\"img/couples_tabs_act.png\") no-repeat";
    } else {
        document.getElementById("tabCompany").style.background = "url(\"img/couples_tabs.png\") no-repeat";
    }
    if (activeId == "tabServices") {
        document.getElementById("tabServices").style.background = "url(\"img/couples_tabs_act.png\") no-repeat";
    } else {
        document.getElementById("tabServices").style.background = "url(\"img/couples_tabs.png\") no-repeat";
    }
    if (activeId == "tabGallery") {
        document.getElementById("tabGallery").style.background = "url(\"img/couples_tabs_act.png\") no-repeat";
    } else {
        document.getElementById("tabGallery").style.background = "url(\"img/couples_tabs.png\") no-repeat";
    }

    document.getElementById("vendorsCompanyPane").style.display = (activeId == "tabCompany")? "block" : "none";
    document.getElementById("vendorsServicesPane").style.display = (activeId == "tabServices")? "block" : "none";
    document.getElementById("vendorsGalleryPane").style.display = (activeId == "tabGallery")? "block" : "none";
    
    document.getElementById("gallery_links").style.display = (activeId == "tabGallery")? "block" : "none";
    document.getElementById("common_links").style.display = (activeId == "tabGallery")? "none" : "block";
    
    changeSubTab("subPaneVideos");
    
    document.getElementById("expandCollapseDiv").style.display = (activeId == "tabServices")? "block" : "none";
    
    if (activeId != "tabServices") {
        showHidePlayerFixed(true);
    }
}
function changeSubTab(activeId) {
    document.getElementById("subPanePictures").style.display = (activeId == "subPanePictures")? "block" : "none";
    document.getElementById("subPaneVideos").style.display = (activeId == "subPaneVideos")? "block" : "none";
    
    document.getElementById("subTabPictures").style.textDecoration = (activeId == "subPanePictures")? "underline" : "none";
    document.getElementById("subTabVideos").style.textDecoration = (activeId == "subPaneVideos")? "underline" : "none";
    
    if (activeId == "subPaneVideos") {
        dataProvider.populateVendorVideosPane();
        
        try {
            //document.getElementById("Zoom").farCallPlay();
        } catch (e) {
            //
        }
    } else if (activeId == "subPanePictures") {
        dataProvider.populateVendorPicsPane();
        
        try {
            //document.getElementById("Zoom").farCallPause();
        } catch (e) {
            //
        }
    }
    
    if (activeVideo != null) {
        selectVideo(document.getElementById("vendor_video_" + activeVideo));
    }
}
function showHidePlayerFixed(hiddenPlayer) {
    document.getElementById("Zoom").style.visibility = (hiddenPlayer)? "visible" : "hidden";
    document.getElementById("flashPlayer").style.height = (hiddenPlayer)? "331px" : "1px";
    
    document.getElementById("vendorsCompanyPane").style.height = (!hiddenPlayer)? "460px" : "165px";
    document.getElementById("vendorsServicesPane").style.height = (!hiddenPlayer)? "460px" : "130px";
    document.getElementById("vendorsGalleryPane").style.height = (!hiddenPlayer)? "460px" : "130px";
    
    document.getElementById("showHideText").innerHTML = (!hiddenPlayer)? "-Collapse text and show player" : "+Expand text and hide player";
}
function showHidePlayer() {
    showHidePlayerFixed(document.getElementById("Zoom").style.visibility == "hidden");
}
function selectVideo(obj, videoIndex) {
    for (var i = 0; i < 8; i++) {
        try {
            if (document.getElementById("vendor_video_" + i)) {
                document.getElementById("vendor_video_" + i).innerHTML = "";
            }
        } catch (e) {
            //
        }
    }
    
    try {
    	obj.innerHTML = "<span></span>";
    } catch (e) {
    	//
    }
    
    if (videoIndex) {
        try {
            document.getElementById("Zoom").farCallJumpToVideo(videoIndex);
        } catch (e) {
            //
        }
    }
}
function selectPic(obj, picIndex) {
    for (var i = 0; i < 8; i++) {
        try {
            if (document.getElementById("vendor_pic_" + i)) {
                document.getElementById("vendor_pic_" + i).innerHTML = "";
            }
        } catch (e) {
            //
        }
    }
    
    try {
    	obj.innerHTML = "<span></span>";
    } catch (e) {
    	//
    }
    
    if (picIndex) {
        try {
            document.getElementById("Zoom").farCallJumpToVideo(picIndex);
        } catch (e) {
            //
        }
    }
}
function farCallVideoChange(index) {
    activeVideo = index;
    
    var type = null;
    if (index >= dataProvider.vendorVideos.length) {
        type = "pic";
    } else {
        type = "video";
    }

    var page = null;
    var videoIndex = null;
    
    document.getElementById("subPanePictures").style.display = (type == "pic")? "block" : "none";
    document.getElementById("subPaneVideos").style.display = (type == "video")? "block" : "none";
    
    document.getElementById("subTabPictures").style.textDecoration = (type == "pic")? "underline" : "none";
    document.getElementById("subTabVideos").style.textDecoration = (type == "video")? "underline" : "none";
    
    if (type == "video") {
        page = parseInt(index / 8) + 1;
        videoIndex = index - (page - 1) * 8;
        
	    gotoVideoPage(page);
	    selectVideo(document.getElementById("vendor_video_" + videoIndex));
    } else if (type == "pic") {
        page = parseInt((index - dataProvider.vendorVideos.length) / 8) + 1;
        videoIndex = (index - dataProvider.vendorVideos.length) - (page - 1) * 8;
        
        gotoPicPage(page);
        selectPic(document.getElementById("vendor_pic_" + videoIndex));
    }
}
function share() {
    shareWin.open();
    document.getElementById("status").innerHTML = (document.getElementById("status").innerHTML == "[sent!]")? "" : document.getElementById("status").innerHTML;
}

function doShareSend() {
	document.getElementById("sMsg").innerHTML = "";
    var sender = document.getElementById("sender").value;
    var receiver = document.getElementById("receiver").value;
    var message = document.getElementById("message").value;
    
    if (sender && receiver && message) {
        var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(receiver)) {
            document.getElementById("status").innerHTML = "[sending...]";
            
            sendMail.callback = function() {
                document.getElementById("status").innerHTML = "[sent!]";
	            document.getElementById('sender').value = '';
	            document.getElementById('receiver').value = '';
	            //document.getElementById('message').value = '';
	            document.getElementById("status").innerHTML = '';                
            }
            
            var senderText = sender + " has sent you this wedding link";
            var messageText = message + "\r\n" + BASE_URL + "view-vendors/" + dataProvider.categoryId + "/" + dataProvider.vendorId + ".html";
            sendMail.send(senderText, receiver, messageText);
        } else {
        	document.getElementById("sMsg").innerHTML = "Email is invalid!";
            document.getElementById("receiver").focus();
        }
    } else {
    	document.getElementById("sMsg").innerHTML = "All fields are required!";
    }
}
