﻿/// <reference path="jquery.intellisense.js" />

var shortcutsBarHeight = 0;
var silverlightInstalled = false;
var galleries;

$(document).ready(function() {
    InitSectionStyles();
    InitShortcutsBar();
    InitContentSwitchers();
    InitGlobalElements();
    InitGalleries();
    InitEmailDefuscation();
    CheckNavHeight();
});

ToggleExpandableListItems = function(el, show) {
    // override for sitefinity's expandable list control, fixes a few bugs
    if (show)
        $(el).parent().next('.sf_expandableList').find('.sf_listItemBody').show();
    else
        $(el).parent().next('.sf_expandableList').find('.sf_listItemBody').hide();

    $(el).hide();
    $(el).siblings().show();
}

LeftNavItemClicking = function(s, e) {
    e.get_item().set_expanded(!e.get_item().get_expanded());
}

SilverlightInstalled = function(installed) {
    silverlightInstalled = installed;
}

InitSectionStyles = function() {
    $('.pmv-Section').each(function(i) {
        if ($(this).find('.pmv-Section-Body').length == 0) {
            $(this).wrapInner('<div class="pmv-Section-Body"></div>').prepend('<div class="pmv-Section-Header"><div class="hack-cornerLeft"><div class="hack-cornerRight"></div></div></div>').append('<div class="pmv-Section-Footer"><div class="hack-cornerLeft"><div class="hack-cornerRight"></div></div></div>');
            $(this).find('.pmv-Section-Body').append('<div class="hack-clearBoth"></div>');
        }
        $(this).css('visibility', 'visible');
    });
}

CheckNavHeight = function() {
    if (($('.pmv-NavSecondary').height() >= $('.pmv-ContentContainer').height()) && ($('.pmv-NavSecondary').height() > 0)) {
        if ($.browser.msie && $.browser.version <= 6)
            $('.pmv-ContentContainer').height($('.pmv-NavSecondary').height() + 20);
        else
            $('.pmv-ContentContainer').css('min-height', $('.pmv-NavSecondary').height() + 20);
    }
}

InitEmailDefuscation = function() {
    $('.email').defuscate().show();
}

LaunchMapModal = function(address) {
    $.modal('<iframe scrolling="no" frameborder="0" marginheight="0" marginwidth="0" height="400px" width="100%" src="' + MapURL + '?address=' + address + '" />', {closeTitle: 'Close'});
}

LaunchEventDetailsModal = function(id) {
    LaunchModalFromURL(EventDetailsURL + '?' + id);
}

LaunchModalFromURL = function(url) {
    $.modal($('#pmv-ModalContainer').load(url), {closeTitle: 'Close'});
}

InitGalleries = function() {
    galleries = new Array();

    $('.pmv-GalleryPhotos').each(function(i) {
        galleries[i] = new ImageGallery($(this).attr('id'), i);
    });
}

InitGlobalElements = function() {
    $('.pmv-Section').each(function(i){
        $(this).find('h2:first').addClass('first');
    });
    
    $('.pmv-ContentSecondary, .pmv-ContentPrimary').find('h2:first').addClass('first');
}

InitShortcutsBar = function() {
    shortcutsBarHeight = $('#pmv-Shortcuts').height();
    $('#pmv-Shortcuts').css({height: 0, visibility: 'visible'});
    
    $('#pmv-ShortcutsBar').click(function(e){
        $(this).toggleClass('expanded');
        
        
        if ($(this).hasClass('expanded')) {
            $("#pmv-Shortcuts").animate({ 
                height: shortcutsBarHeight + 'px'
            }, 300 );
            
            $("#pmv-ShortcutsBar").animate({ 
                marginTop: shortcutsBarHeight + 'px'
            }, 300 );
        }
        else {
            $("#pmv-Shortcuts").animate({ 
                height: '0'
            }, 100 );
            
            $("#pmv-ShortcutsBar").animate({ 
                marginTop: '0'
            }, 100 );
        }
        
        return false;
    });
}

InitContentSwitchers = function() {
    $('.pmv-ContentSwitcher li.item').each(function(i){
        $(this).mouseover(function(e){
            if (!$(this).hasClass('selected'))
                $(this).addClass('hover');
        });
        $(this).mouseout(function(e){
            $(this).removeClass('hover');
        });
        $(this).click(function(e){
            $('.pmv-ContentSwitcher li.item').removeClass('selected');
            $('.pmv-ContentSwitcher li.item .description').hide();
            $('.pmv-ContentSwitcher li.item:eq(' + i + ')').addClass('selected');
            $('.pmv-ContentSwitcher li.item:eq(' + i + ') .description').show();
            
        });
    });
    
    $('.pmv-ContentSwitcher li:first .description').show();  //show first item
    
    
    // now the tab switchers
    
    
    var str = '<ul class="tabNav">';
    $('.pmv-TabSwitcher .tabNav').each(function(i){
        str += '<li>' + $(this).html() + '</li>';
    }).remove();
    str += '</ul>'
    
    $('.pmv-TabSwitcher').prepend(str);
    
    $('.pmv-TabSwitcher .tabNav a').each(function(i){
        $(this).append('<span></span>');
        
        $(this).click(function(e){
            $('.pmv-TabSwitcher .tabNav li').removeClass('selected');
            $('.pmv-TabSwitcher .tabNav li:eq(' + i + ')').addClass('selected');
            $('.pmv-TabSwitcher .tabContent').hide();
            $('.pmv-TabSwitcher .tabContent:eq(' + i + ')').show();
            return false; 
        });
    });
    $('.pmv-TabSwitcher .tabNav li:eq(0)').addClass('selected');
    $('.pmv-TabSwitcher, .pmv-TabSwitcher .tabContent:first').show();
}




/* utility functions */

jQuery.fn.defuscate = function() {
    return this.each(function(){ 
        var href = $(this).attr('href').split('/');
        var email = String(href[href.length-1]).replace(/\s*\(.+\)\s*/, "@"); 
        $(this).before('<a href="javascript: void(0);" onclick="convertAddyHref(this, \'' + email + '\')">' + $(this).html() + '</a>').remove();
    }); 
}
convertAddyHref = function(el, email) {
    $(el).attr('href', 'mailto:' + email);
}