/* Init JQuery plugins & widgets */
$(document).ready(function(){
    /* Not erady for this yet
    $('.aThumb').click(function() { return( jqImgViewer(this)); });
    $('.ivPreview').click(function() { return( jqViewer(this));  });
    */
    // so assign the ColorBox event to elements
    $("a[rel='colorbox']").colorbox({slideshow:false, fixed:true, width:'80%', height:'90%'  });

});
/* ===============================================================================
 revealer plugin - attaches a 'reveal' link to a selection. The link will open/close
 an associated element when clicked. The association is based on a rel attribute.

 Usage:
 <X class='blockTitle' rel='block_1'>My Block Title</X>
 <X id='block_1' class='blockContent'>My Block Content</X>

 $('.blockTitle').revealer();
===============================================================================*/
(function( $ ){

    $.fn.revealer = function( options ) {

        var defaults  = {
        'appendTag'     : 'a',
        'openFirst'     : false,
        'addTriggers'   : true,
        'exclusiveOpen' : false,
        'openID'        : false,
        'slide'         :  true,
        'fade'          :  false,
        'speed'         : '800',
        'openText'      : 'Open',
        'closeText'     : 'Close'
        };

        // If options exist,  merge them with default settings
        var options = $.extend(defaults, options);

        // DOnt open first element if an openID has been passed
        if (options.openID) options.openFirst = false;

        // Process each element in the selection
        this.each(function(index) {

            obj = $(this);
            //alert(obj.attr('rel'));
            var relBlockID = obj.attr('rel'); // id of the block rel-ated to this selection
            var block = $('#' + relBlockID); // the block rel-ated to this selection
            if (index == 0  &&  options.openFirst) {
                // add reveal triggers unless they are already setup
                if (options.addTriggers) obj.append('<a class="revealer revealed">' + options.closeText + '</a>');// Attach the revealer element
                else obj.addClass('revealed');
            }
            else{
                block.addClass('concealed').hide();  // add the concealed class and hide the block
                if (options.addTriggers) obj.append('<a class="revealer concealed">' + options.openText + '</a>');// Attach the revealer element
                else obj.addClass('concealed');
            }
        });

        // Start of  click handler attacjed to all the revealers
        $('.revealer').click(function() {
            var clicker = $(this);

            // if exclusiveOpen = true close all open blocks
            if (options.exclusiveOpen){
               $('.revealer.revealed').each(function(index) {
                  $.fn.revealer.close($(this));
               });

            }

            if (clicker.hasClass('concealed')){ // SHOW IT
               $.fn.revealer.open(clicker);
            }
            else {                              // HIDE It
               $.fn.revealer.close(clicker);
            }
            return(false);
        });
        // End of  click handler attacjed to all the revealers

        //  Open block  function
        $.fn.revealer.open = function(clicker) {
            if (options.addTriggers) {
                var parent =clicker.parent();
                var blockID = parent.attr('rel');
            }
            else blockID = clicker.attr('rel');
            var block = $('#' + blockID);
            if (options.slide) block.slideDown(options.speed) ;
            if (options.fade) block.fadeIn(options.speed) ;
            block.removeClass('concealed').addClass('revealed');
            clicker.removeClass('concealed').addClass('revealed');
            if (options.addTriggers) clicker.html(options.closeText);      // show close text

        };


         //  Close  block function
        $.fn.revealer.close = function(clicker) {
            if (options.addTriggers) {
                var parent =clicker.parent();
                var blockID = parent.attr('rel');
            }
            else blockID = clicker.attr('rel');
            var block = $('#' + blockID);
            block.removeClass('revealed').addClass('concealed');
            clicker.removeClass('revealed').addClass('concealed');
            if (options.addTriggers) clicker.html(options.openText);    // show open text
            if (options.slide) block.slideUp(options.speed).return() ;
            if (options.fade) block.fadeOut(options.speed).return() ;
        }

        // OPen an element if a openID has been passed
        if (options.openID){
               $('#' + options.openID).find(".revealer").click() ;
        }
    };
})(jQuery);
/* ===============================================================================
 iFrame plugin -
 Usage:
    $.fn.jqIframe(href, {options });
===============================================================================  */
/* Shorthand calls */
function jqIFrame(url, ifWidth, ifHeight){ return ( $.fn.jqIframe(url, {width:ifWidth, height: ifHeight }) ); }
function jqIFrameClose(reloadParent){
    $.fn.jqIframe.closePopup();
    if (reloadParent) window.location.reload();;
}
(function( $ ){

    $.fn.jqIframe = function( url, options ) {

        var defaults  = {
        'width'     : '640',
        'height'    : '480',
        autoHeight  :true
        };

        // If options exist,  merge them with default settings
        var options = $.extend(defaults, options);

        /* =========== INTERNAL FUNCTIONS START ============== */
        //  Show Mask  function
        $.fn.jqIframe.showMask = function() {
            // Create mask
            $('<div/>', {
                name: 'jqIFrameMask',
                id:   'jqIFrameMask',
                style: 'position: absolute; top:0; left:0; display:none; width:100%; height:100%; background: #333;z-index: 99999; '
            }).appendTo('body');

            //Get the document height and width
            var maskHeight = $(document).height();
            var maskWidth = $(window).width();

            //Set height and width to mask to fill up the whole screen
            $('#jqIFrameMask').css({'width':maskWidth,'height':maskHeight});

            $('#jqIFrameMask').fadeTo("500",0.8);
        };

        //  Remove popup & mask  function
        $.fn.jqIframe.closePopup = function() {
            $('#jqIFramePopup').fadeOut().remove();
            $('#jqIFrameMask').fadeOut().remove();
        };

        /* =========== INTERNAL FUNCTIONS END ============== */

        // Create iFrame
        $('<iframe />', {
            name: 'jqIFramePopup',
            id:   'jqIFramePopup',
            src:   url,
            frameborder: 0,
            allowtransparency: 'true',
            style: 'border: none; background: transparent;  position: absolute; display:none; width:' + options.width + 'px; height:' + options.height + 'px;z-index: 999999;'
        }).appendTo('body');

        // reset the iframe height on load to the document height?
        if (options.autoHeight){
            $('iframe#jqIFramePopup').load(function(){
                this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
                parent.$('#jqIFramePopup').jqCentre();
            });
        }

        // create close button and add to popup
        $('iframe#jqIFramePopup').load(function()
        {
            var oBody = $("#jqIFramePopup").contents().find("body");
            oBody.append('<a id="jqIFrameCloseBtn" onclick="parent.$.fn.jqIframe.closePopup();" href="#"></a>');
        });


        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $('#jqIFramePopup').jqCentre();

        $('#jqIFramePopup').css("position","absolute");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
        this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");


        // Show  teh mask
        $.fn.jqIframe.showMask();


        // if mask is clicked close it
        $('#jqIFrameMask').click(function () {  $.fn.jqIframe.closePopup();    });
        // or if user presses Esc
        $(document).keypress(function(e){ if(e.keyCode==27 ){ $.fn.jqIframe.closePopup(); }});

        // SHow the popup
        $('#jqIFramePopup').fadeIn("500");

        return(false); // prevent click actions
    };
})(jQuery);

jQuery.fn.jqCentre = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

