/*
 * Global Variables
 */
var TOTAL_NUMBER_OF_LAWYERS    = '46';
var TOTAL_NUMBER_OF_PARALEGALS =  '6';
var TOTAL_NUMBER_OF_LAWCLERKS  = '30';
var TOTAL_SUPPORT_STAFF        = '22';

function inc(varName) {
    document.write(varName);
}



/*
 * Clears the search input box
 */
function clearSearchField(inputbox) {
   if (inputbox) {
       if (inputbox.value=="Search") {
           inputbox.value="";
       }
   }
}


/*
 * jQuery plugin to center a floating div on the screen.
 */
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

/*
 * jQuery plugin to vertically center a div in another div.
 */
jQuery.fn.vAlign = function() {
    return this.each(function(i) {
        var h = $(this).height();
        var oh = $(this).outerHeight();
        var mt = (h + (oh - h)) / 2;
        $(this).css("margin-top", "-" + mt + "px");
        $(this).css("top", "50%");
        $(this).css("position", "absolute");
    });
};



/*
 * jQuery plugin to turn a span or div into a facebook link.
 */
jQuery.fn.facebook = function () {
    this.css("width","50px");
    this.css("height","50px");
    this.css("text-align","center");
    this.css("padding-top", "3px");
    this.css("cursor", "pointer");
    this.attr("title", "Click to visit our facebook page");
    this.css("background-repeat","no-repeat");
    this.css("background-image","url(/lib/images/facebook.png)");
    this.bind("click", function() {
        window.open("http://www.facebook.com/pages/Toronto-ON/Devry-Smith-Frank-LLP/54364521511?v=wall","Facebook");
    });
    return this;
}

    
/*
 * You tube launcher jQuery
 */
YouTubeLauncher = function(id, caption) {
    var ytl = this;
    ytl.id = id;
    ytl.caption = caption;
    ytl.content = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/'+ytl.id+'?fs=1&amp;hl=en_US&amp;autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ytl.id+'?fs=1&amp;hl=en_US&amp;autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';
    
};

YouTubeLauncher.prototype.play = function() {
    var ytl = this;
    var $x = $("#close-youtube");
    $("#close-youtube").center();
    $("#youtube-player-container").center();
    $("#youtube-player-container").corner("round");
    $('#youtube-caption').html(ytl.caption);
    $("#youtube-player").show();
    $("#youtube-player-container").fadeTo(1000, .8,
        function() {
            $("#youtube-player").html(ytl.content);                
            var positionOfPlayerContainer = $("#youtube-player-container").position();
            if (positionOfPlayerContainer.left != 0) {
                $("#close-youtube").css("left", positionOfPlayerContainer.left+75);
                $("#close-youtube").css("top", positionOfPlayerContainer.top+420);
                $("#close-youtube").fadeTo(1000, .90);
            }
        }
    );
};

YouTubeLauncher.prototype.init = function() {
  var ytl = this;
  $(".close-youtube").corner("round");         
  $("#close-youtube-btn").click(function() {
                                    $("#close-youtube").hide();
                                    $("#youtube-player").hide();
                                    $("#youtube-player").html("");
                                    $("#youtube-player-container").fadeTo(500, 0, function() {
                                                                                    $("#youtube-player-container").hide();
                                                                                   });
                         });
};


Testimonial = function($container, categories) {
    var self = this;
    self.count = 0;
    self.categories = categories;
    self.$container = $container;
};

Testimonial.prototype.startTransitions = function() {
    var self = this;
    self.execute = function() {
        self.count += 1;
        $.ajax({
            type : 'POST',
            dataType: 'json',
            url  : '/lib/php/testimonials.php', 
            data : {categories: self.categories}, 
            success: function(data) { 
                try{
                    var testimonialText = '';
                    var byLine = '';
                    var wrapper = '';
                    if (data.text && '' != data.text) {
                        testimonialText = '<div class="testimonialText">&quot;'+data.text+'&quot;</div>';
                    }
                    if (data.author && '' != data.author) {
                        byLine = '<div  class="testimonialByLine">~ ' + data.author + '</div>';
                    }
                    if (testimonialText != '') {                            
                        var now = new Date();
                        var $previousWrapper = $(".testimonial-wrapper");
                        var wrapperId = '' + now.getMilliseconds() + '';
                        var wrapper = '<div id="testimonial_' + wrapperId + '" class="testimonial-wrapper" style="display:none"><div class="testimonial-content">' + testimonialText+byLine + '</div></div';
                        if ($previousWrapper.length > 0) {
                            self.$container.append(wrapper);
                            var $currentWrapper = $("#testimonial_" +wrapperId);
                            $currentWrapper.vAlign();
                            $previousWrapper.fadeOut(4000,function() {$(this).remove()} );
                            $currentWrapper.fadeIn(3000);
                        } else {
                            self.$container.append(wrapper);
                            var $currentWrapper = $("#testimonial_"+wrapperId);
                            $currentWrapper.vAlign();
                            if ($currentWrapper.length > 0) {
                                $currentWrapper.fadeIn(3000);
                            }
                        }                    
                    }    
                } catch (e) {
                    setTimeout(self.execute, 0);                    
                    console.log(e);
                    return;
                }                
                setTimeout(self.execute, data.delay);
            } // end:success
        }); // end:ajax      
    } // end:self.execute

    self.execute();
};

/**
  * Testimonials Cross Fader Plugin
  * @author Mark Eldridge mark.eldridge@gmail.com
  */

(function( $ ){
    
    $.fn.testimonial = function(relevantCategories) {
        var self = this;
        self.$transitionBox = $(this);
        self.testimonial = new Testimonial(self.$transitionBox, relevantCategories);
        self.testimonial.startTransitions();
    };
    
    
    
})( jQuery );





/**
  *  Plugin for converting names into references to vCards
  *
  */

(function( $ ){
    $.fn.vcard = function() {
        var $divBox = $(this);    
        var $originalName = jQuery.trim($divBox.attr("tag"));
        var $identityName = $originalName;
        if ($originalName != "") {
            $identityName = $identityName.replace(" ", "_");
            var html = "<div style='width:230px;text-align:center'><br/><a style='color:inherit;font-size:inherit;font-weight:inherit;' class='vcard-link' href='http://www.devrylaw.ca/vcards?id=" + $identityName + "' title='Download Virtual Business Card for '>Click to download vCard:&nbsp;<br><img style='border:none;' src='/lib/images/qrcodes/vcard.png'></a><br/><br/>Scan to download vCard:<br><img style='width:130px' src='/lib/images/qrcodes/" +$identityName+ ".png'></div>";
            $divBox.html(html);
        }
    };
})( jQuery );




/*
 * Common document ready functions to be fired from header every time.
 * eg) Starting up the chrome menu.
 *     Initializing the Newsletter subscribe dialog box actions.
 */

$(document).ready(function() {

    cssdropdown.startchrome("chromemenu");
    startTransitions();

    $('.subscribeButton').click(function(e) {        
        var $dialog = $('#subscribeDialogOuter');
        $dialog.show();        
        var height = $(window).height();
        var width = $(document).width();                
        $dialog.css({
            'left' : width/2 - ($dialog.width() / 2),  // half width - half element width
            'top' : height/2 - ($dialog.height() / 2), // similar
            'z-index' : 15,                        // make sure element is on top
        });
        
        $('#emailAddress').focus();
    });
    $('#goButton').click(function() {
        $('#subscribeDialogOuter').hide();
        alert("Thank you for subscribing.\nA confirmation email will be sent to you shortly.");
    });
    $('#closeButton').click(function() {
        $('#subscribeDialogOuter').hide();
    });

    var tab_id = "#tab" + postId;
    if (tab_id != '#tab' && $(tab_id).length > 0) {
        if ($(tab_id).hasClass('small')) {
            $(tab_id).addClass('tab-enabled-small');
        } else if ($(tab_id).hasClass('medium')) {
            $(tab_id).addClass('tab-enabled-medium');
        } else {
            $(tab_id).addClass('tab-enabled');
        }
    }

    $(".facebook-link").facebook();

    $('div.vcard').vcard();

    var $articles = $(".create-article");    

    var articleNum = 0;
    $articles.each(function() {
        try {
            articleNum++;
            var $article = $(this);
            var content = $article.html();
            var articleWrapper = "<div class='dsf-article'><div class='article-header'/><div class='article-body' id='articleBody_"+articleNum+"'></div><div class='article-footer'/></div>";
            $contentArea = $("#bottomColumnHome");
            $contentArea.append(articleWrapper);
            document.getElementById("articleBody_"+articleNum).innerHTML = content;
            $article.html("");
        } catch (e) {
        }        
    });
});


/**
  *  Image slideshow defaults.
  *
  */
$.fn.cycle.defaults = { 
    timeout:       8000,  // milliseconds between slide transitions (0 to disable auto advance) 
    speed:         1000,  // speed of the transition (any valid fx speed value) 
    next:          null,  // id of element to use as click trigger for next slide 
    prev:          null,  // id of element to use as click trigger for previous slide 
    before:        null,  // transition callback (scope set to element to be shown) 
    after:         null,  // transition callback (scope set to element that was shown) 
    height:       'auto', // container height 
    sync:          1,     // true if in/out transitions should occur simultaneously 
    fit:           0,     // force slides to fit container 
    pause:         0,     // true to enable "pause on hover" 
    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
    slideExpr:     null   // expression for selecting slides (if something other than all children is required) 
};

function startTransitions() {
    $('#imageTransitionContainer').cycle();
};

/**
  *  Pops open a booking calendar for the appropriate calendar ID.
  *
  */
function openCalendar(id) {
    var url = "http://www.devrylaw.ca/bookings/calendar.html?cid=" + id;
    var options = "scrollbars=0,status=0,location=0,resizable=0,toolbar=0,menubar=0,height=360,width=300";
    window.open(url, "Availability", options);
}
