// JavaScript Document
jQuery.noConflict();
jQuery(document).ready(function () {

    /* VIDEO LIST */

    if (jQuery('a.videoLink').length > 0) {
        //jQuery('a.videoLink').prepend('<div class="playButton"></div>');
        jQuery('a.videoLink').each(function () {
            jQuery(this).attr('href', jQuery(this).attr('href') + ';autoplay=1')
        });

        jQuery('a.videoLink').click(function () {
            jQuery.fancybox({
                'padding': 0,
                'autoScale': false,
                'transitionIn': 'none',
                'transitionOut': 'none',
                'title': this.title,
                'width': 640,
                'height': 385,
                'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type': 'swf',
                'swf': {
                    'wmode': 'transparent',
                    'allowfullscreen': 'true'
                }
            });

            return false;
        });
    }


    function displayYoutubeThumbnails() {
        jQuery(".videoItem .videoImgContainer").each(function (i) {
            if (jQuery(this).children("#hdnURL") != '') {
                if (jQuery(this).children("#hdnURL").length > 0) {
                    var url = jQuery.jYoutube(jQuery(this).children("#hdnURL").val());
                    jQuery(this).children("a").append(jQuery('<img width="170" src="' + url + '" />'));
                    jQuery(this).children("a").append('<div class="playButton small"></div>');
                }
            }
        });
    }

    jQuery(".videoImage .videoImgContainer").each(function (i) {
        if (jQuery(this).children("#hdnURL") != '') {
            if (jQuery(this).children("#hdnURL").length > 0) {
                var url = jQuery.jYoutube(jQuery(this).children("#hdnURL").val());
                jQuery(this).children("a").append(jQuery('<img width="370" src="' + url + '" />'));
                jQuery(this).children("a").append('<div class="playButton"></div>');
            }
        }
    });

    displayYoutubeThumbnails();

    jQuery('#ddlLocations').change(function () {
        displayVideos();
        InitializeGoogleMap();
    });

    jQuery('#ddlKeywords').change(function () {
        displayVideos();
        InitializeGoogleMap();
    });

    jQuery('#ddlWorkplaces').change(function () {
        displayVideos();
        InitializeGoogleMap();
    });

    function displayVideos() {
        var searchQueryURL = jQuery("#hItemPath").attr("value") + "?p=aj";
        if (jQuery("#ddlLocations").length != 0) {
            var index = jQuery("#ddlLocations").attr('selectedIndex');
            if (index != "" && index != "0") {
                searchQueryURL += "&location=" + encodeURI(jQuery('#ddlLocations option:selected').val());
            }
        }
        if (jQuery("#ddlKeywords").length != 0) {
            index = jQuery("#ddlKeywords").attr('selectedIndex');
            if (index != "" && index != "0") {
                searchQueryURL += "&keyword=" + encodeURI(jQuery('#ddlKeywords option:selected').val());
            }
        }
        if (jQuery("#ddlWorkplaces").length != 0) {
            index = jQuery("#ddlWorkplaces").attr('selectedIndex');
            if (index != "" && index != "0") {
                searchQueryURL += "&workplace=" + encodeURI(jQuery('#ddlWorkplaces option:selected').val());
            }
        }
        jQuery.post(searchQueryURL, function (data) {
            jQuery("#videoContainer").html(data);
            displayYoutubeThumbnails();
        });
    }

    /*Google Maps*/
    function InitializeGoogleMap() {
        if (jQuery("#institutionsMap").length > 0) {

            var xmlFile = jQuery("#hXMLPath").attr("value");
            if (jQuery("#ddlLocations").length != 0) {
                var index = jQuery("#ddlLocations").attr('selectedIndex');
                if (index != "" && index != "0") {
                    xmlFile += "&location=" + encodeURI(jQuery('#ddlLocations option:selected').val());
                }
            }
            if (jQuery("#ddlKeywords").length != 0) {
                index = jQuery("#ddlKeywords").attr('selectedIndex');
                if (index != "" && index != "0") {
                    xmlFile += "&keyword=" + encodeURI(jQuery('#ddlKeywords option:selected').val());
                }
            }
            if (jQuery("#ddlWorkplaces").length != 0) {
                index = jQuery("#ddlWorkplaces").attr('selectedIndex');
                if (index != "" && index != "0") {
                    xmlFile += "&workplace=" + encodeURI(jQuery('#ddlWorkplaces option:selected').val());
                }
            } 
            InstitutionsLocator.initialize(xmlFile);
        }
    }

    InitializeGoogleMap();


    jQuery(".scfSectionContent").addClass("clearFix");

    jQuery('#tabMenuLinks li img, #medlemsservice ul li, #subFrontpage #linkList ul li').pngFix();

    jQuery("#tabMenuContent > li:not(:first)").hide();
    jQuery("#tabMenuLinks li:first").addClass("activeTab");
    jQuery("#tabMenuLinks li:last").addClass("lastChild");




    jQuery("#tabMenuLinks li").click(function () {
        jQuery("#tabMenuLinks li.activeTab").removeClass("activeTab");
        jQuery(this).addClass("activeTab");


        currentActiveTabNr = jQuery("#tabMenuLinks li").index(this);


        jQuery("#tabMenuContent > li").hide();
        jQuery("#tabMenuContent > li").eq(currentActiveTabNr).show();


    });

    var inputValue = '';

    jQuery(".input.focus#name, .input.focus#email, .input.focus#subject, .input.focus#subject, textarea#comment").focus(function () {
        inputValue = jQuery(this).val();
        jQuery(this).val('')
    });

    jQuery(".input.focus#name, .input.focus#email, .input.focus#subject, .input.focus#subject, textarea#comment").blur(function () {
        if (jQuery(this).val() == '') {
            jQuery(this).val(inputValue);
        }
    });





    // if there are less than three teasers on a row, we need to clear the content or it won't take into consideration the .graphicTeaser{margin-bottom: 10px;}
    /*
    if(jQuery.browser.msie){
    jQuery(".teasers").append('<br style="clear: both; />"');
    }
    */
});

