var ops = {

    layout :
    {
        init : function() {

            window.$page = { 
                win        : $(window),
                body       : $('body'),
                a          : $('a'),
                cont       : $('#content'), 
                ajc        : $('#ajc'), 
                wrappad    : $('.wrapper-pad'),
                td         : $('.td'),
                player     : $('.player'),
                miniplayer : $('.miniplayer'),
                loader     : $('.loader'),
                prop       : $('.prop'),
                jmail      : $('.jmail'),
                fb         : $('.form-button'),
                items      : $('.items'),
                item       : $('.item'),
                price      : $('.price'),
                box_round  : $('.blue-box-full, .grey-box-full'),
                top_round  : $('.blue-box, .grey-box'),
                bot_round  : $('.blue-box-bottom, .grey-box-bottom'),
                bot        : $('.bottom'),
                mg         : $('.items').find('.minigutter'),
                ql         : $('.quicklink')
            };

            _op();
            _fp();
            _url_ajc();
            _jmail();
            _price_display();
            _prop();
            _prop_items();
            _fonts();
            _plat();
            _corners();

            $('a[rel="external"]').live('click', function(e) {
                e.preventDefault();
                return _window_open($(this).attr('href'));
            });

            $page.ql.live('click', function(e){
                e.preventDefault();
                return _quicklink(this);
            });

            $page.fb.click(function(e){
                $(this).closest('form').submit();
                $(this).blur();
            });

            var resizeTimer = null;
            $page.win.bind('resize', function() {
                if (resizeTimer) { clearTimeout(resizeTimer); }
                resizeTimer = setTimeout(_op, 100);
            });
        }
    }, 
    nav : 
    {
        init : function() {

            window.$mininav = { 
                info        : $('#info'), 
                head        : $('#info h3'), 
                ajc         : $('.click-aj'), 
                autolink    : $('#info .auto_link'), 
                tog         : $('#info dt.tog')
            };

            $mininav.head.live('click', function(e){
                return _close_ajc();
            });
            $mininav.ajc.live('click', function(e){
                e.preventDefault();
                return _ajc($(this).attr('href'));
            });
            $mininav.autolink.live('click', function(e){
                _auto_link(this);
                return this;
            });
            $mininav.tog.live('click', function(e){
                e.preventDefault();
                return _toggle_info(this);
            });
        }
    },
    form : 
    {
        init : function() {

            window.$elem = { 
                cform        : $('#contact_form'), 
                reset        : $('#reset'), 
                confirm      : $('#confirm'), 
                tncs_label   : $('#tncs_cb-label'),
                tncscb       : $('#tncs_cb'),
                selreveal    : $('select.select-reveal'),
                cbradio      : $('input.cbradio'),
                is_default   : $('input.is_default')
            };

            $elem.is_default.live('click', function(e){
                return false;
            });
            $elem.cbradio.live('click', function(e){
                return _form_cbradio(this);
            });
            $elem.cform.submit(function(e){
                return _form_validate(this);
            });
            $elem.reset.live('click', function(e){
                return _form_reset(this);
            });
            $elem.tncs_label.live('click', function(e){
                _ajc('/tncs/');
                return true;
            });
            $elem.selreveal.live('change', function(e){
                return _form_sel_reveal(this);
            });
        }
    },
    widget : 
    {
        init : function() {

            window.$box = { 
                modal      : $('#modal'), 
                oplayer    : $('.overlay-player')
            };

            _overlay();
            _tabpane();
        }
    }
};

_tabpane = function() {

	$('#Videos').tabs('div.pane', { event : 'mouseover' });
};

_op = function() {

    if( $(window).width() > 1040 ) {
        $('.wrapper').css({ margin : '0 auto' });
    } else {
        $('.wrapper').css({ margin : '0' });
    }

    $page.wrappad.each(function(e) {
        $(this).find('.tr').each(function(h) {
            $(this).append('<div class="td space">&nbsp;</div>');
        });
    });
};

_prop = function() { 

    $page.prop.each(function(e) { 

        dlw     = $(this).width();
        dds     = $(this).find('dd');
        img     = $(this).find('.image');
        copy    = $(this).find('.copy');
        cols    = dds.length;
        if( img.length > 0 && copy.length > 0 ) 
        { 
            iw = (img.find('img').width()) ? img.find('img').width():null;
            if( iw ) 
            {
                cw = dlw-iw;
                img.css({ width : iw+'px' });
                copy.css({ width : (cw-4)+'px' });
            }
        }
        else 
        {
            w = '50%';
            if( $(dds).filter(':last').length > 0 ) {
                r = dlw;
                r -= $(dds).filter(':last').width();
                w = (r<dlw) ? r+'px':w;
            }
            dds.filter(':first').css({ width : w });
        }
    });
};


_prop_items = function() { 

    iw = 960;
    ih = $page.item.height();

    l  = $page.item.length;

	gs = ((l-1)*10);

	iw -= gs;

    w  = Math.floor(iw/l);

    $page.mg.filter(':last').remove();

    if(w > 0 && $page.items.length > 0 ) 
    {
        $page.item.each(function(e) {
            $(this).css({ width : w+'px' });
			$(this).find('table').css({ width : (w-30)+'px', height : ih+'px' });
        });
    }
};

_corners = function() {

    if( $.browser.msie && $.browser.version < 7 ) 
    {
        return false;
    }

    $page.box_round.each(function(e) {
        $(this).corner('5px');
    });

    $page.top_round.each(function(e) {
        $(this).corner('top round 5px');
    });

    $page.bot_round.each(function(e) {
        $(this).corner('bottom round 5px');
    });
};

_price_display = function() {

    $page.price.each(function(e) {

        em         = $(this).find('em');
        span     = em.find('span');
        price     = span.text();
        if( price != '') 
        {
            ex         = price.split('.');
            money     = ( ex[0] ) ? ex[0]:null;
            ext     = ( ex[1] ) ? ex[1]:null;

            if(money !== null) 
            {
                span.html(money);
            }

            if(ext !== null) 
            {
                sup = $('<sup>'+ext+'</sup>');

                em.after(sup);
            }
        }
    });
};

_overlay = function() {

    $('a.overlay-click').each(function(e) {

        rel = $(this).attr('rel');

        $('a[rel='+rel+']').overlay({
                            expose: { loadSpeed: 200, opacity: 0.5 },
                            onLoad: function() { _fp_stop_all(); }
                        });
        $("div.modal").appendTo("body").corner('8px');

    });
};

_fp_stop_all = function() {
    $f("*").each(function(i) { 
        $f().stop();
    });
};

_fp = function() {

    var playas = [];

    $page.player.each(function(i) {

        id   = $(this).attr('id');
        rel  = $(this).attr('rel');
        args = rel.split('|');
        w    = (args['0']) ? args['0']:300;
        h    = (args['1']) ? args['1']:150;
        tn   = (args['2']) ? args['2']:'';

        playas.push(id);

        $f(id, 
            {
                src     : 'http://cdn.turnhere.com/player/current/flowplayer.swf',
                width   : w,
                height  : h,
                wmode   : 'opaque',
                quality : 'autohigh'
            },
            {
                key     : '#@da34d01747bd077f2e4',
                plugins : {
                    controls: { height : 23 },
                    limelite : {
                                url : 'http://cdn.turnhere.com/player/current/flowplayer.rtmp.swf',
                                netConnectionUrl : 'rtmp://turnhere.fcod.llnwd.net/a732/o1/'
                    }
                },
                playlist : [
                                { 
                                    url : tn, 
                                    fit : 'orig' 
                                }, 
                                { 
                                    provider : 'limelite', 
                                    autoPlay : false, 
                                    bufferLength : 3 
                                }
                            ],
                onStart  : function() { 
                    _trackEvent('Video', 'play', playas[i]); 
                },
                bwcheck: {   
                    url: '/assets/swf/flowplayer.bwcheck-3.1.1.swf',  
                    serverType: 'fms', 
                    dynamic: false, 
                    netConnectionUrl: 'rtmp://turnhere.fcod.llnwd.net/a732/o1/', 
                    bitrates: [150, 400, 700, 1000]
                },
                
                version: [9, 119], 
                
                // older versions will see a custom message 
                onFail: function()  { 
                    document.getElementById("info").innerHTML = 
                        "You need the latest Flash version to see MP4 movies. " + 
                        "Your version is " + this.getVersion() 
                    ; 
                }
            }
        );
    });

    $page.miniplayer.each(function(i) {

        id   = $(this).attr('id');
        rel  = $(this).attr('rel');
        args = rel.split('|');
        w    = (args['0']) ? args['0']:300;
        h    = (args['1']) ? args['1']:150;
        tn   = (args['2']) ? args['2']:'';

        playas.push(id);

        $f(id, 
            {
                src     : 'http://cdn.turnhere.com/player/current/flowplayer.swf',
                width   : w,
                height  : h,
                wmode   : 'opaque',
                quality : 'autohigh'
            },
            {
                key     : '#@da34d01747bd077f2e4',
                plugins : {
                    controls: { 
                                height : 15,
                                scrubber : false
                    },
                    limelite : {
                                url : 'http://cdn.turnhere.com/player/current/flowplayer.rtmp.swf',
                                netConnectionUrl : 'rtmp://turnhere.fcod.llnwd.net/a732/o1/'
                    }
                },
                playlist : [
                                { 
                                    url : tn, 
                                    fit : 'orig' 
                                }, 
                                { 
                                    provider : 'limelite', 
                                    autoPlay : false, 
                                    bufferLength : 3 
                                }
                            ],
                onStart  : function() { 
                    _trackEvent('Video', 'play', playas[i]); 
                },
                bwcheck: {   
                    url: '/assets/swf/flowplayer.bwcheck-3.1.1.swf',  
                    serverType: 'fms', 
                    dynamic: false, 
                    netConnectionUrl: 'rtmp://turnhere.fcod.llnwd.net/a732/o1/', 
                    bitrates: [150, 400, 700, 1000]
                },
                
                version: [9, 119], 
                
                // older versions will see a custom message 
                onFail: function()  { 
                    document.getElementById("info").innerHTML = 
                        "You need the latest Flash version to see MP4 movies. " + 
                        "Your version is " + this.getVersion() 
                    ; 
                }
            }
        );
    });
};

_url_ajc = function() {

    url = document.location.href;
    ex  = url.split('#');
    uri = (ex[1]) ? ex[1]:null;

    if( uri === null ) 
    {
        return false;
    }

    arr = [0, 'tos', 1, 'tncs', 2, 'faq', 3, 'privacy'];

    if( $.inArray(uri, arr) ) 
    {
        _ajc('/'+uri+'/');
    }
};

_quicklink = function(ele) { 
    uri = $(ele).attr('href');
    $.ajax({ 
        type: 'POST', 
        url: '/ql/', 
        data: 'uri='+uri,
        success: function(msg) { 
            location.href = msg;
        }
    });
    return false;
};

_ajc = function(href) {

    $.ajax({ 
        type: 'POST', 
        url: href, 
        beforeSend: function() {
            _loader(true);
            if( $mininav.info.length > 0 ) 
            {
                $page.ajc.fadeOut(100);
            }
        },
        success: function(msg) { 
            $page.ajc.html(msg);

            if( $mininav.info.length > 0 ) 
            {
                $page.ajc.fadeIn(100);
            } 
            else 
            {
                $page.ajc.slideDown(500);
            }

        },
        complete: function() { 
            _jmail();
            _loader(false);
            _scroll_top();
        }
    });

    return false;
};

_close_ajc = function() {
    _loader(true);
    $page.ajc.slideUp(500);
    _loader(false);
};

_auto_link = function(ele) {
    id = $(ele).attr('rel');
    obj = $('#'+id);
    if( obj.length > 0 ) 
    {
        $(obj).next('dl').find('dt:first').css({ textDecoration : 'blink' });
        $(obj).next('dl').find('dd:first').slideDown(300);
    }
};

_toggle_info = function(ele) {

    _loader(true);

    dl = $(ele).closest('dl');

    var index = $('dt').index(ele);
    $(dl).find('dd').each(function(i) {
        if( $('dd').index(this) == index )
        {
            $(this).slideDown(300);
        } 
        else 
        { 
            $(this).slideUp(300);
        }
    });

    _loader(false);
};


_jmail = function() {
    $page.jmail.each(function(i) {
        e = $(this).text().split('|');
        if(!e[1]) {
            return false;
        }
        un = e[0];
        dn = e[1];
        dm = e[2];
        if(!un || !dn || !dm) {
            $(this).html('<a name="">no email</a>');
            return false;
        }
        str = un+'@'+dn+'.'+dm;
        tx  = (e[3]) ? e[3]:str;
        $(this).html('<a href="mailto:'+str+'">'+tx+'</a>');
        return false;
    });
};

_plat = function() {
    if( ( /mac/.test(navigator.userAgent.toLowerCase())) ) {
        $page.cont.addClass('bgcm').removeClass('bgc');
        $page.body.addClass('bcm').removeClass('bc');
    }
};

_loader = function(tog) {

    if( $('#loader').length === 0 ) 
    {
        loa = $('<div id="loader" />');
        $('body').append(loa);
    }

    if(tog === false)
    {
        $('#loader').hide();
    } 
    else 
    {
        $('#loader').show();
        $(document).mousemove(function(e){

            $("#loader").css({
                top: (e.pageY - 18) + "px",
                left: (e.pageX - 7) + "px"
            });
        });

    }
};

_scroll_top = function() { 
    $('html, body').animate({ scrollTop : 0 }, 'slow');
};

_gb_togable = function(obj, flag) {

	if(flag === false) 
	{
        obj.attr('disabled', true).addClass('gb-disabled');
        _timer(3, 'gb_togable', obj);
	}
	else 
	{
        obj.attr('disabled', false).removeClass('gb-disabled');
	}
};

_timer = function(secs, type, var1) {
    if(secs > 0) 
    { 
        setTimeout((function() 
        { 
            secs--;
            _timer(secs, type, var1);
        }), 1000);
    } 
    else 
    {
        switch(type) 
        {
            case 'gb_togable':
                _gb_togable(var1, true);
            break;
        }
    }
};

_window_open = function(href, name, options) {
    if(href === null) 
    {
        return;
    }

    name     = (name) ? name:'new';
    options = (options) ? options:'';
    window.open(href, name, options);
};

_fonts = function() { 

    if( $.browser.msie && $.browser.version < 7 ) 
    {
        return false;
    }

    Cufon.replace('.tgf', { fontFamily : 'Trade Gothic Medium' });
    Cufon.replace('.tgbf', { fontFamily : 'Trade Gothic Bold 2' });
    Cufon.replace('.nav', { fontFamily : 'Trade Gothic Bold 2', textShadow : ('#666 1px 1px') });
    if( $.browser.msie ) {
        Cufon.now();
    }
};

_form_cbradio = function(ele) {

    if( $elem.cbradio.length > 1 ) 
    {
        $elem.cbradio.each(function(i) {
            if( $(this).is(':checked') ) 
            {
                $(this).attr('checked', false);
            }
        });
    
        $(ele).attr('checked', true);
    } 
    else 
    {
        return true;
    }

    _form_check_defaults();
}; 

_form_check_defaults = function(ele) {

    $elem.is_default.each(function(i) {
        if( $(this).is(':checked') == false ) 
        {
            $(this).attr('checked', true);
        }
    });
}; 

_form_sel_reveal = function(ele) {

    val = $(ele).val();
    rel = $(ele).attr('rel');
    id  = $(ele).attr('id');

    if(id) 
    {
        if( rel == val ) 
        {
            _sel_toggle_other(id, true);
        } 
        else 
        {
            _sel_toggle_other(id, false);
        }
    }
    return true;
};

_sel_toggle_other = function(str, flag) {

    obj = $('#dl_'+str+'_other');
    if(obj.length > 0) 
    {
        if(flag) 
        {
            $(obj).fadeIn(500).find('.text').attr('disabled', false);
        }
        else 
        {
            $(obj).fadeOut(500).find('.text').attr('disabled', true);
        } 
    }
};

_form_validate = function(ele) {

    _gb_togable($elem.confirm, false);

    if( ! $elem.tncscb.is(':checked') ) 
    {
        alert('You must check The Terms & Conditions checkbox.');
        return false;
    }

    return true;
}; 

_form_reset = function(ele) {
    location.href = $(ele).attr('rel');
};



/*    END JS    */