/*
 * Facebox (for jQuery)
 * version: 1.1 (03/01/2008)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/facebox/
 *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2007, 2008 Chris Wanstrath [ chris@ozmm.org ]
 *
 * Usage:
 *  
 *  jQuery(document).ready(function() {
 *    jQuery('a[rel*=facebox]').facebox() 
 *  })
 *
 *  <a href="#terms" rel="facebox">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="terms.html" rel="facebox">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="terms.png" rel="facebox">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 * 
 *    jQuery.facebox('some html')
 *
 *  This will open a facebox with "some html" as the content.
 *    
 *    jQuery.facebox(function() { ajaxes })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */
(function(jQuery) {
    jQuery.facebox = function(data, klass) {
        jQuery.facebox.init()
        jQuery.facebox.loading()
        jQuery.isFunction(data) ? data.call(jQuery) : jQuery.facebox.reveal(data, klass)
    }

    jQuery.facebox.settings = {
        loading_image : '../imagesloading.gif',
        close_image   : '../images/closelabel.gif',
        image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
        facebox_html  : '\
  <div id="facebox" style="display:none;"> \
    <div class="popup"> \
      <table> \
        <tbody> \
          <tr> \
            <td class="tl"/><td class="b"/><td class="tr"/> \
          </tr> \
          <tr> \
            <td class="b"/> \
            <td class="body" > \
              <div class="content" > \
              </div> \
              <div class="footer"> \
                <a href="#" class="close"> \
                  <img src="../images/closelabel.gif" title="close" class="close_image" /> \
                </a> \
              </div> \
            </td> \
            <td class="b"/> \
          </tr> \
          <tr> \
            <td class="bl"/><td class="b"/><td class="br"/> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div>'
    }

    jQuery.facebox.loading = function() {
        if (jQuery('#facebox .loading').length == 1) return true

        jQuery('#facebox .content').empty()
        jQuery('#facebox .body').children().hide().end().
        append('<div class="loading"><img src="'+jQuery.facebox.settings.loading_image+'"/></div>')

        var pageScroll = jQuery.facebox.getPageScroll()
        jQuery('#facebox').css({
            top:	pageScroll[1] + (jQuery.facebox.getPageHeight() / 10),
            left:	pageScroll[0]
        }).show()

        jQuery(document).bind('keydown.facebox', function(e) {
            if (e.keyCode == 27) jQuery.facebox.close()
        })
    }

    jQuery.facebox.reveal = function(data, klass) {
        if (klass) jQuery('#facebox .content').addClass(klass)
        jQuery('#facebox .content').append(data)
        jQuery('#facebox .loading').remove()
        jQuery('#facebox .body').children().fadeIn('normal')
    }

    jQuery.facebox.close = function() {
        jQuery(document).trigger('close.facebox')
        return false
    }

    jQuery(document).bind('close.facebox', function() {
        jQuery(document).unbind('keydown.facebox')
        jQuery('#facebox').fadeOut(function() {
            jQuery('#facebox .content').removeClass().addClass('content')
        })
    })

    jQuery.fn.facebox = function(settings) {
        jQuery.facebox.init(settings)

        var image_types = jQuery.facebox.settings.image_types.join('|')
        image_types = new RegExp('\.' + image_types + 'jQuery', 'i')

        function click_handler() {
            jQuery.facebox.loading(true)

            // support for rel="facebox[.inline_popup]" syntax, to add a class
            var klass = this.rel.match(/facebox\[\.(\w+)\]/)
            if (klass) klass = klass[1]

            // div
            if (this.href.match(/#/)) {
                var url    = window.location.href.split('#')[0]
                var target = this.href.replace(url,'')
                jQuery.facebox.reveal(jQuery(target).clone().show(), klass)

            // image
            } else if (this.href.match(image_types)) {
                var image = new Image()
                var found = false;
                image.onload = function() {
                    jQuery.facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
                }
                var contenthtml = '';
                if (this.href.indexOf(".youtube.") > 0)
                {
                    found = true;
                    contenthtml = '<div style="border:solid 1px grey;width:660px;height:525px;padding-top:10px;padding-left:20px;top:-10px;left:-10px;background-color:black"> \
                                        <object width="640" height="505"> \
                                        <param name="movie" value="http://www.youtube.com/v/U6RxisKULpc&hl=en&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param>\n\
                                        <param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/U6RxisKULpc&hl=en&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>\n\
                                     </div>'
                    jQuery.facebox.reveal(contenthtml, klass)
                }
                if (this.href.indexOf(".demorequest.") > 0)
                {
                    found = true;
                    contenthtml = '<iframe src="http://spreadsheets.google.com/embeddedform?key=tvKhbpFHmYH86K6xBfSUWqQ" scrolling=no width="608" height="608" frameborder="0" marginheight="0" marginwidth="0">Loading...</iframe>';
                    jQuery.facebox.reveal(contenthtml, klass);
                }
                if (this.href.indexOf(".aboutus.") > 0)
                {
                    found = true;
                    contenthtml  = '<div style="width:750px;height:450px;">';
                    contenthtml  += '<span style="color: darkgrey; font-size: 24px;">About ORbyte Solutions...</span><br/>';
                    contenthtml  += '<span style="font-size: 16px; font-family:calibri,tahoma,arial, sans-serif;">ORbyte Solutions is technology firm focusing on delivering the best-of-breed software to the financial sector.<br/><br/>';
                    contenthtml  += 'The company was founded in 2008 by a senior executive team with more than 10 years experience in financials services.&nbsp;In fact, the team respectively operated at the highest level of management in organisation such as E*Trade, Barclays Capital, Cargill and City Index.&nbsp;'
                    contenthtml  += 'They have a very successful track record in the delivery of realtime systems for these majors financial institutions worldwide.<br/><br/>';
                    contenthtml  += 'After a year of reasearch and development, ORbyte brings to you the "Web2.0 trading experience":&nbsp;<b>Trade2.0</b><br/><br/>';
                    contenthtml  += '</span>';
                    contenthtml  += '<span style="font-size: 14px; font-family:calibri,tahoma,arial, sans-serif;">';
                    contenthtml  += '<center><img src="images/orbytealpha_small.jpg"/><br/>';
                    contenthtml  += 'ORbyte Solutions Ltd<br/>';
                    contenthtml  += '11, Hastings Street<br/>';
                    contenthtml  += 'London<br/>';
                    contenthtml  += 'SE18 6SY<br/>';
                    contenthtml  += 'info@orbytesolutions.com<br/>';
                    contenthtml  += '<a href="http://twitter.com/orbyte" target="_blank" style="position:relative;top:10px;text-decoration:none;"><img  src="images/icon-twitter.png" style="position:relative;top:0px;"/></a>&nbsp;&nbsp;';
                    contenthtml  += '<a href="http://www.facebook.com/group.php?gid=216996944744" style="position:relative;top:10px;text-decoration:none" target="_blank"><img src="images/icon-facebook.png" style="position:relative;top:0px;"/></a>&nbsp;&nbsp;';
                    contenthtml  += '<a href="http://www.linkedin.com/companies/orbyte-solutions-ltd" style="position:relative;top:8px;text-decoration:none" target="_blank"><img src="images/icon-linkedin.png" style="position:relative;top:0px;"/></a>&nbsp;&nbsp;';
                    contenthtml  += '<a href="http://www.youtube.com/orbytesolutions" style="position:relative;top:10px;text-decoration:none" target="_blank"><img src="images/icon-youtube.png" style="position:relative;top:0px;"/></a>&nbsp;&nbsp;</span>';
                    contenthtml  += '</center></span>';
                    contenthtml  += '</div>';
                    jQuery.facebox.reveal(contenthtml, klass);
                }


                if (this.href.indexOf(".screenshots.") > 0)
                {
                    found = true;
                    contenthtml  = '<div style="width:740px;height:550px">';
                    contenthtml += '<ul id="screenshot_rotate" style="list-style: none;">';
                    contenthtml += '<li><img src="carousel/screenshots/widgetcompil1_large.png"/></li>';
                    contenthtml += '<li><img src="carousel/screenshots/portal0_large.png"/></li>';
                    contenthtml += '<li><img src="carousel/screenshots/portal1_large.png"/></li>';
                    contenthtml += '<li><img src="carousel/screenshots/menus1_large.png"/></li>';
                    contenthtml += '<li><img src="carousel/screenshots/widgetcompil2_large.png"/></li>';
                    contenthtml += '<li><img src="carousel/screenshots/widgetcompil5_large.png"/></li>';
                    contenthtml += '<li><img src="carousel/screenshots/widgetcompil4_large.png"/></li>';
                    contenthtml += '<li><img src="carousel/screenshots/widgetcompil3_large.png"/></li>';
                    contenthtml += '</ul>';
                    contenthtml += '</div>';
                    jQuery.facebox.reveal(contenthtml, klass);
                    $('#screenshot_rotate').innerfade({
                        speed: 'slow',
                        timeout: 4000,
                        type: 'sequence',
                        containerheight: '370px'
                    });
                }
                if (false == found)
                {
                    image.src = this.href
                }



            // ajax
            } else {
                jQuery.get(this.href, function(data) {
                    jQuery.facebox.reveal(data, klass)
                })
            }

            return false
        }

        this.click(click_handler)
        return this
    }

    jQuery.facebox.init = function(settings) {
        if (jQuery.facebox.settings.inited) {
            return true
        } else {
            jQuery.facebox.settings.inited = true
        }

        if (settings) jQuery.extend(jQuery.facebox.settings, settings)
        jQuery('body').append(jQuery.facebox.settings.facebox_html)

        var preload = [ new Image(), new Image() ]
        preload[0].src = jQuery.facebox.settings.close_image
        preload[1].src = jQuery.facebox.settings.loading_image

        jQuery('#facebox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
            preload.push(new Image())
            preload.slice(-1).src = jQuery(this).css('background-image').replace(/url\((.+)\)/, 'jQuery1')
        })

        jQuery('#facebox .close').click(jQuery.facebox.close)
        jQuery('#facebox .close_image').attr('src', jQuery.facebox.settings.close_image)
    }

    // getPageScroll() by quirksmode.com
    jQuery.facebox.getPageScroll = function() {
        var xScroll, yScroll;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
            xScroll = self.pageXOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
            xScroll = document.documentElement.scrollLeft;
        } else if (document.body) {// all other Explorers
            yScroll = document.body.scrollTop;
            xScroll = document.body.scrollLeft;
        }
        return new Array(xScroll,yScroll)
    }

    // adapter from getPageSize() by quirksmode.com
    jQuery.facebox.getPageHeight = function() {
        var windowHeight
        if (self.innerHeight) {	// all except Explorer
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowHeight = document.body.clientHeight;
        }
        return windowHeight
    }
})(jQuery);

