function setTabRollOverImages() {
    $(".tabImage").each(function(i) {
        var imageFile = $(this).attr("src");
        var overImage = new Image();
        overImage.src = themePath + "layout_images/" + twoLetterUICulture + "/tabs_b" + (i + 1) + "-active-trans.png";

        $(this).hover(
            function() {
                $(this).attr("src", overImage.src);
            },
            function() {
                $(this).attr("src", imageFile);
            });
    });
}

function setSearchFilter(filter) {
    $(".commonSearch").each(function() {
        var href = $(this).attr("href");
        var filterPos = href.lastIndexOf("&f=");
        if (filterPos > 0) {
            href = href.substring(0, filterPos);
            href += "&f=" + filter;
        }

        $(this).attr("href", href);
    });

    for (var i = 1; i < 5; ++i) {
        if (i == filter) {
            $("#tabImage" + i).attr("src", themePath + "layout_images/" + twoLetterUICulture + "/tabs_b" + i + "-selected-trans.png");
        } else {
            $("#tabImage" + i).attr("src", themePath + "layout_images/" + twoLetterUICulture + "/tabs_b" + i + "-trans.png");
        }
    }

    setTabRollOverImages();
}

function attachSearchFunctionality() {
    $("#searchButton").bind("click", performSearch);
}

function performSearch() {
    var searchPage = searchUrl + "?";
    searchPage += "q=" + escape($("#searchText").attr("value"));
    searchPage += "&";
    searchPage += "f=" + $("#searchFilter").attr("value");
    window.location = searchPage;
    return false;
}

function setFormDefaultButtons() {
    $(".form input, .form select")
        .live('keypress', function(e) {
            var defaultButton = $(this).parents('.form').find('.defaultButton');
            if (defaultButton.length <= 0) {
                return true;
            }

            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                defaultButton.click();
                return false;
            } else {
                return true;
            }
        });
}

function attachDatePickerFunctionality() {
    $(".datePicker").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: "dd.mm.yy",
        showOn: 'both',
        buttonImageOnly: true,
        buttonImage: themePath + 'layout_images/calendar.png'
    });
}

function attachRoundedButtonCorners() {
    $(".roundedButton").corners("transparent");
}

function reattachJQueryBehaviors(sender, args) {
    if (args.get_error() == undefined) {
        attachJQueryBehaviors();
    }
    else {
        // Handle ajax errors
        //   alert("There was an error" + args.get_error().message);
    }
}

function showUpDownControls() {
    $(".upDownControl1_1000").spin({
        max: 1000,
        min: 1,
        timeInterval: 200,
        timeBlink: 200,
        imageBasePath: themePath + 'layout_images/'
    });

    $(".upDownControl0_1000").spin({
        max: 1000,
        min: 0,
        timeInterval: 200,
        timeBlink: 200,
        imageBasePath: themePath + 'layout_images/'
    });

    $(".upDownControl1_10").spin({
        max: 10,
        min: 1,
        timeInterval: 200,
        timeBlink: 200,
        imageBasePath: themePath + 'layout_images/'
    });
}

function setupCascadingCountryCity() {
    $(".countryList").change(function() {
        var selectedIndex = this.selectedIndex;
        var selectedOption = this.options[selectedIndex];

        if (selectedOption.value == "Alta tara") {
            $(".otherCountry").show();
            $(".cityList").hide();
            $(".otherCity").show();
        } else {
            $(".cityList").show();
            $(".otherCountry").hide();
            $(".otherCity").hide();

            $.each($(".cityList"), function() {
                toggleCityListAndOtherCity($(this))
            });
        }
    });

    $(".cityList").change(function() {
        toggleCityListAndOtherCity($(this));
    });
}

function toggleCityListAndOtherCity(cityList) {
    var selectedIndex = cityList.attr("selectedIndex");
    var selectedOption = cityList.attr("options")[selectedIndex];

    if (selectedOption.value == "Alt oras") {
        $(".otherCity").show();
    } else {
        $(".otherCity").hide();
    }
}

function attachMainMenu() {
    var menuItems = new Array();
    $.makeArray($("#header a").each(function(i) {
        menuItems[i] = $.trim($(this).text());
    }));

    menuItems = menuItems.join("`");

    var menuUrls = new Array();
    $.makeArray($("#header a").each(function(i) {
        menuUrls[i] = $.trim($(this).attr("href"));
    }));

    menuUrls = menuUrls.join("`");

    //$("#header").empty();

    var flashvars = {
        images: headerImages.join("`"),
        imageDelay: "4",
        labels: menuItems,
        links: menuUrls,
        currentLanguage: twoLetterUICulture,
        selectedIndex: "2"
    };
    var params = {
        swLiveConnect: "true",
        allowFullScreen: "false",
        scale: "noscale",
        menu: "false",
        quality: "best"
    };
    var attributes = {
        id: "header",
        name: "header"
    };
    swfobject.embedSWF("celendo-header.swf", "header", "930", "200", "9.0.0", "js/expressInstall.swf", flashvars, params, attributes);
}

function attachLargeBanners() {
    var links = new Array();
    $.makeArray($("#largeBanner a").each(function(i) {
        links[i] = $.trim($(this).attr("href"));
    }));

    links = links.join("`");

    var texts = new Array();
    $.makeArray($("#largeBanner a img").each(function(i) {
        texts[i] = escape($.trim($(this).attr("alt")));
    }));

    texts = texts.join("`");

    var images = new Array();
    $.makeArray($("#largeBanner a img").each(function(i) {
        images[i] = $.trim($(this).attr("src"));
    }));

    images = images.join("`");

    $("#largeBanner").empty();

    var flashvars =
		        {
		            images: images,
		            texts: texts,
		            links: links
		        };

    var params = {
        swLiveConnect: "true",
        allowFullScreen: "false",
        scale: "noscale",
        menu: "false",
        quality: "best"
    };
    var attributes = {
        id: "largeBanner",
        name: "largeBanner"
    };
    swfobject.embedSWF("celendo-banner.swf", "largeBanner", "930", "140", "9.0.0", "js/expressInstall.swf", flashvars, params, attributes);
}

function attachJQueryBehaviors() {
    attachSearchFunctionality();
    setTabRollOverImages();
    setFormDefaultButtons()
    attachDatePickerFunctionality();
    attachRoundedButtonCorners();
    showUpDownControls();
    setupCascadingCountryCity();
}

function MailPage() {
    mail_str = "mailto:?subject=Link util pentru tine: " + document.title;
    mail_str += "&body=Salut!<br />Ma gandeam ca esti interesat de urmatorul link:<br />";
    mail_str += "<a href='" + location.href + "'>" + location.href + "</a>";
    location.href = mail_str;
}

$(document).ready(function() {
    attachJQueryBehaviors();
    attachMainMenu();
    attachLargeBanners();        
    setTimeout("if(typeof(Sys) != \"undefined\") {Sys.WebForms.PageRequestManager.getInstance().add_endRequest(reattachJQueryBehaviors);}", 200);    
});