function cases_initCallback(carousel) {
    $('.studies-nav .btn-prev').bind('click', function() {
        carousel.stopAuto();
        carousel.prev();
        return false;
    });
    $('.studies-nav .btn-next').bind('click', function() {
        carousel.stopAuto();
        carousel.next();
        return false;
    });
}

function sideSlider_initCallback(carousel) {
    $('.prev-study').bind('click', function() {
          carousel.prev();
          return false;
    });
    $('.next-study').bind('click', function() {
          carousel.next();
          return false;
    });
}

function scrollToLightbox(){
    //scroll to the top of the box
    $(document).scrollTo( $('#facebox'), 1000, {axis: 'y', offset: {top: -30}});
}

function scrollToPageHead(){
    //scroll to the top of the box
    $(document).scrollTo( $('#page-head'), 1000, {axis: 'y'});
}

// Our home featured slider
var home_slider;

$(function(leng) {

    if($('#force-view').length){
        //used for the sbs order forms
        if($('#force-view').text()!=""){
            $(document).scrollTo( $('#force-view'), 1000, {axis: 'y'});
        }
    }

    // Main nav dropdowns
    $('#nav ul > li').hover(function() {
        $(this).find('a:eq(0)').addClass('hover');
        $(this).find('.dd').addClass('dn').removeClass('hide').fadeIn('fast');
    }, function() {
        $(this).find('.hover').removeClass('hover');
        $(this).find('.dd').fadeOut('fast', function() {
            $(this).removeClass('dn').addClass('hide');
        });
    });
    $('.solution').hover(function() {
        $(this).addClass('hover');
        $(this).addClass('hovered');
    }, function() {
        $(this).removeClass('hover');
        $(this).removeClass('hovered');
    });
    $('.hoverable').hover(function() {
       $(this).toggleClass('hovered');
    });
    $('#nav .dd a').not('[rel="facebox"]').bind('click', this, function(e){
      e.stopImmediatePropagation();
      return true;
    });
    $('#nav .dd').live('click', function(e) {
        var hre = $(this).find('a[class*="btn"]').attr('href');
        if(($(e.target).parents('.solution').length > 0) || $(e.target).is('.solution')){
            hre = $(e.target).closest('.solution').find('a[class*="btn"]').attr('href');
        }
        document.location = hre;
        return false;
    });
    $('#nav .dd a[class*="btn"]').hover(function() {
        $(this).parents('.hoverable:eq(0)').toggleClass('btn-hovered');
        $(this).parents('.solution:eq(0)').toggleClass('btn-hovered');
    });

    $('.chat-link').click(function(){
        var url = $(this).attr('href');
        url = url.replace(/http(s|):/, document.location.protocol);

        var win = window.open(url, "Wpromote Live Chat", "width=750,height=600,top=12,left=12,location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=yes");
        win.focus();

        return false;
    });

    /* init advanced slider */
    home_slider = $('#advanced_slider .slider-container').advancedSlider({
        alignType: 'centerCenter',
        effectType: 'simpleSlide',
        fadeSlideshowControls: false,
        height: 273,
        horizontalSlices: 1,
        navigationArrows: true,
        navigationButtonsCenter: false,
        sliceFade: false,
        slideMask: true,
        slicePattern: 'topToBottom',
        slideshow: true,
        slideshowControlsCenter: false,
        slideshowDelay: 7000,
        slideStartPosition: 'left',
        timerAnimation: false,
        verticalSlices: 1,
        width: 980
    });
    $('#advanced_slider .clickable').live('click', function() {
        var hre = $(this).find('a[class*="btn"]').attr('href');
        if(typeof(_gaq)!='undefined' && _gaq.push){
            //should only exist for the live site
            var label = $(this).attr('id');
            _gaq.push(['_trackEvent', 'Banner', 'Click', label]);
        }
        document.location = hre;
        return false;
    });

    // Case studies slider
    $('.case-studies').jcarousel({
        visible : 1,
        scroll : 1,
        animation : 400,
        easing : "easeInOutSine",
        wrap : "circular",
        initCallback : cases_initCallback,
        buttonPrevHTML : null,
        buttonNextHTML : null
    });

    // Awards
    $('.awards-holder').click(function(){
        document.location = $(this).find('#awards-go').attr('href');
        return false;
    });
    $('.award-holder a.notext').mouseover(function() {
        $(this).parent().addClass('hovered');
        $(this).parent().find('.award-hover').stop(true,true).fadeIn('fast');
    });
    $('.award-hover').mouseleave(function() {
        $(this).fadeOut('fast', function() {
            $(this).parent().removeClass('hovered');
        });
    });

    // Popups
    var forms_to_track = ['consultation-form', 'sbs-consultation', 'sbs-contact', 'inquiry'];
    $('a[rel="facebox"]').facebox().click(function(){
        //catch ajax forms for google event tracking
        var label = $(this).attr('href').split(/\//g).pop();
        if($.inArray(label, forms_to_track)!=-1){
            trackEvent("Form", "Open", label, null);
        }
    });
    $(document).bind('loading.facebox', function() {

        if($('#advanced_slider').length){
            // pause the homepage slider if a lightbox is opened
            $('#advanced_slider .slideshow-controls').removeClass('pause').addClass('play');
            home_slider.pauseSlideshow();
        }
        $('.tooltip').hide();

    });
    $(document).bind('beforeReveal.facebox', function() {

    });

    $(document).bind('close.facebox', function() {
        $('.tooltip').hide();
        if($.facebox.settings.closeTimerID){
             clearTimeout($.facebox.settings.closeTimerID);
             $.facebox.settings.closeTimerID = null;
        }
    });

    // Forgot Password
    $('#login #show-password-form').live('click', function(){
        $('.popup-t h2').fadeOut('fast', function() {
            $(this).text('Reset Password').fadeIn('fast');
        });
        $('#login').slideUp(function() {
            $('#reset-pass').slideDown();
        });
        return false;
    })

    // Hover states for input buttons
    $('input[type="submit"]').live('mouseover', function() {
        $(this).addClass('hover');
    }).live('mouseout', function() {
        $(this).removeClass('hover');
    });
    $('.btn-red14').hover(function() {
        $(this).toggleClass('btn-red14-h');
    });

    // Blink fields (this is being replaced by the field attribute "placeholder")
    $('.blink').live('focus', function() {
        if(this.value == this.title) {
            this.value = ''
        }
    }).live('blur', function() {
        if(this.value == '') {
            this.value = this.title
        }
    });
    
    //Placeholder Support check!
    var input = document.createElement('input');
    if(!('placeholder' in input)){
        $('input[placeholder]').val($(this).attr('placeholder')).
        live('focus', function(){
                if($(this).val() == $(this).attr('placeholder')) {
                        $(this).val("");
                }  
        }).live('blur', function() {
                if($(this).val() == '') {
                        $(this).val($(this).attr('placeholder'));
                }
        });
    }

    // Blink field counters
    $('.char-count').each(function() {
        var mx = Math.round($(this).attr('maxlength'));
        var ml = Math.round($(this).val().length);
        var mb = Math.round(mx - ml);
        $(this).parent().next().find('span').text(mb)
    });
    $('.char-count').live('keyup', function() {
        var leng = $(this).attr('maxlength');
        var tl = $(this).val().length;
        var result = leng - tl;
        if(result < 0 ){
            $(this).val($(this).val().substr(0, leng));
            $(this).parent().next().find('span').text(0);
        } else {
            $(this).parent().next().find('span').text(result);
        }
    });

    // Trouble logging in form
    $('#request-details .btn-email').click(function() {

        $.post($("#request-details").attr('action'), $("#request-details").serialize(), function(data){
            if(data!=0){
                $('#request-details .t-error:visible').slideUp('fast');
                $('#request-details .t-feedback').slideDown('fast');
            } else {
                $('#request-details .t-error span').text($('#request-details .blink:eq(0)').val()).parent().slideDown('fast');
            }
        });

        return false;
    });

    // Custom checkbox

    /* needs more work for package selection
     *
    $('.custom-checkbox').each(function(){
        $(this).removeClass("checked");
        if($(this).find(':checked').length > 0){
            alert($(this).find(':checked').attr('id'));
            $(this).addClass("checked");
        }
    });

    */

    $('.custom-checkbox').live('click', function() {

        if($(this).parents('#upgraded-dd').length > 0) return; //handle this case seperately

        var check = $(this).find('.checkbox');
        if(check.attr('checked') == false) {
            check.attr('checked', true);
            $(this).addClass('checked');
            if($(this).parents('.card-types').length > 0) {
                if($(this).siblings('.checked').length > 0) {
                    $(this).siblings('.checked').removeClass('checked').find('.checkbox').attr('checked', false);
                }
            }
            if($(this).parents('.current-client').length > 0) {
                $(this).parent().siblings().find('.checked').removeClass('checked').find('.checkbox').attr('checked', false);
            }
            if($(this).parents('.quicklist-experts').length > 0) {
                if($(this).siblings('.checked').length > 0) {
                    $(this).siblings('.checked').removeClass('checked').find('.checkbox').attr('checked', false);
                    if($(this).find('.checkbox').attr('id') == 'own-details') {
                        $(this).parents('.quicklist-experts').find('.website-hidden-fields').slideDown();
                    } else {
                        $(this).parents('.quicklist-experts:eq(0)').find('.website-hidden-fields').slideUp();
                    }
                }
            }
            if($(this).parents('.target-sex').length > 0) {
                $(this).siblings('.checked').removeClass('checked').find('.checkbox').attr('checked', false);
            }
        } else {
            check.attr('checked', false);
            $(this).removeClass('checked');
            if($(this).parents('.quicklist-experts').length > 0) {
                $(this).parents('.quicklist-experts:eq(0)').find('.website-hidden-fields').slideUp();
            }
        }
    });



	$('.checks-holder .label').live('click', function(e) {
                if($(this).find('.checkbox').attr('checked') == false) {
                                $(this).addClass('checked');
                                $(this).find('.checkbox').attr('checked', 'checked')
                } else {
                                $(this).removeClass('checked');
                                $(this).find('.checkbox').attr('checked', false);
                        }

        });

	$('#socialboost-order-form .contact-info input, #quicklist-order-form .contact-info input').change(function(){

		form = $(this).closest('form');

		//only submit partial forms on valid email addresses
		if(window.modules['form'].isValidEmail(form.find('#email').val())){

			$.post(url('small_business_solutions/ajax_incomplete_order'), form.serialize(), function(data){

				//alert(data);

			});

		}

	});
        
        
        var leadcapture_form_active = false;
        
	$('#sbs-consultation #consultation-form input, #leadcapture-form input, #qlpro-form input').change(function(){

		form = $(this).closest('form');
                
                //alert('attempt post...');
		//only submit partial forms on valid email addresses
		if(window.modules['form'].isValidEmail(form.find('.email').val()) && !leadcapture_form_active){
                        
                        //alert('posting data...');
                        leadcapture_form_active = true;

			$.post(url('small_business_solutions/ajax_consultation_submit'), form.serialize(), function(data){
				if(data){
				   form.find('#ContactId').val(data);
				}
                                //alert('post completed!');
                                leadcapture_form_active = false
			});

		}

	});

    $('.btn-red14 label').live('click', function() {
        var check = $(this).parent().find('.checkbox');
        if(check.attr('checked') == false) {
            check.attr('checked', true);
        } else {
            check.attr('checked', false);
        }
    });

    $('.checkbox').live('change', function() {
        if($(this).attr('checked') == false) {
            $(this).parent().removeClass('checked');
        } else {
            $(this).parent().addClass('checked');
        }
    });

    // Popup buttons with custom checkbox
    $('.cbtn').live('click', function(e) {
        if($(e.target).hasClass('custom-checkbox')) {
            return;
        }
        if($(this).find('.checkbox').attr('checked') == true) {
            $(this).find('.checked').removeClass('checked').find('.checkbox').attr('checked', false);
        } else {
            $(this).find('.custom-checkbox').addClass('checked').find('.checkbox').attr('checked', true);
        }
        return false;
    });

    // Popup buttons with custom radio
    $('.rbtn').live('click', function(e) {
        if($(this).find('.radio').attr('checked') == true) {
            // $(this).find('.checked').removeClass('checked').find('.checkbox').attr('checked', false);
            // radio btns dont uncheck
        } else {
            //remove the current checked
            $(this).closest('.rbtn-grp').find('.checked').removeClass('checked').find('.radio').attr('checked', false);
            $(this).find('.custom-radio').addClass('checked').find('.radio').attr('checked', true);
        }
        return false;
    });

    // FAQ
    $('.faq-row-head a').click(function() {
        $(this).parent().toggleClass('opened');
        $(this).parents('.faq-row').find('.faq-row-entry').slideToggle();
        return false;
    });
    $('.faq-close').click(function() {
        $(this).parents('.faq-row').find('.opened').removeClass('opened');
        $(this).parents('.faq-row').find('.faq-row-entry').slideUp();
        return false;
    });

    // Slide-out questions in 2nd popup
    $('.row-how .jquery-selectbox-item').live('click', function() {
        if($(this).text() == 'Tradeshow') {
            $('.optional-rows > div:visible').slideUp();
            $('.row-tradeshow').slideDown();
        }
        else if($(this).text() == 'Existing Client') {
            $('.optional-rows > div:visible').slideUp();
            $('.row-client').slideDown();
        }
        else if($(this).text() == 'Other Referral') {
            $('.optional-rows > div:visible').slideUp();
            $('.row-referral').slideDown();
        }
        else if($(this).text() == 'Other Website') {
            $('.optional-rows > div:visible').slideUp();
            $('.row-website').slideDown();
        }
        else if($(this).text() == 'Other') {
            $('.optional-rows > div:visible').slideUp();
            $('.row-other').slideDown();
        }
        else {
            $('.optional-rows > div:visible').slideUp();
        }
    });
    // Order page packages hover
    $('.box-packages-b > div').hover(function() {
        if($(this).index() > 0) {
            $(this).addClass('col-selected');
        }
    }, function() {
        if($('#plan').val() != $(this).attr('package')) {
            $(this).removeClass('col-selected');
        }
    });

    // Order page tooltips
    $('.help').live('click', function() {
        var t = $(this).attr('rel');
        var pos_l = Math.round($(this).parents('.row').offset().left);
        var pos_t = Math.round($(this).parents('.row').offset().top);
        var pos_ar = Math.round($(this).offset().left);
        var pos_ar2 = pos_ar - pos_l;
        $('.tooltip-i').text(t);
        if($('.tooltip').is(':hidden')) {
            $(this).addClass('help-hov');
            if(pos_ar2 > 244) {
                $('.tooltip .ar').addClass('ar2');
            } else {
                $('.tooltip .ar').removeClass('ar2').css({
                    left : pos_ar2 - 23
                });
            }
            $('.tooltip').css({
                left : pos_l + 17,
                top : pos_t + 44
            }).fadeIn(100);
        }else {
            if($(this).hasClass('help-hov')) {
                $('.tooltip').hide();
                $(this).removeClass('help-hov');
            } else {
                $('.help-hov').removeClass('help-hov');
                $(this).addClass('help-hov');
                $('.tooltip').hide();
                if(pos_ar2 > 244) {
                    $('.tooltip .ar').addClass('ar2');
                } else {
                    $('.tooltip .ar').removeClass('ar2').css({
                        left : pos_ar2 - 23
                    });
                }
                $('.tooltip').css({
                    left : pos_l + 17,
                    top : pos_t + 44
                }).fadeIn(200);
            }
        }
        return false;
    });

    // Billing cols hover
    $('.col-billing').hover(function() {
        $(this).addClass('col-billing-h');
    }, function() {
        if($(this).find('.checkbox').attr('checked') == false) {
            $(this).removeClass('col-billing-h');
        }
    });

    // Init Custom Select box
    $('.custom-select').selectbox();

    // Init custom scroller for Terms & Conditions box in Order page
    $('.the-terms').jScrollPane({
        scrollbarWidth : 25,
        scrollbarMargin : 5,
        showArrows : true,
        arrowSize : 15,
        dragMinHeight : 12
    });

    // Choose Service Popup
    $('.choose-service .box-choose, .box-campaign').live('mouseover', function() {
        $(this).addClass('box-hover');
    }).live('mouseout', function() {
        $(this).removeClass('box-hover');
    }).live('click', function() {
        var href = $(this).find('a[class*="btn"]').attr('href');
        document.location = href;
        return false;
    });

    // Init Datepicker
    $('.datepicker').datepicker({dateFormat: "mm-dd-yy"});

    $('#seo-tabs').tabs({fx: {height: 'toggle'}});

    // Expand and collapse tabs on home page
    $('.expand-tab').click(function() {
    	var el = $(this).prev('.hidden-part');
    	if(el.is(':hidden')) {
    		$(this).text('Collapse').addClass('collapsed-tab').prev().slideDown();
    	}else{
    		$(this).text('Expand').removeClass('collapsed-tab').prev().slideUp();
    	}
    	return false;
    });



    // Show ad options
    $('.ad-box-holder .select-ad').live('click', function() {

        var action = $('#select-ad-action').val();
        var ad_id = $(this).attr('ad_id');

        $('.ad-box-holder .ad-preview-container').each(function(){
            $(this).removeClass('selected');
        })
        $(this).parent().find('.ad-preview-container').addClass('selected');

        $.post(action, {id: ad_id}, function(data){

            $('#sb-tabs-container').fadeOut('fast', function(){
                $('#sb-tabs-container').html(data);

                // Init Tabs
                $('#sb-tabs').tabs({fx: {height: 'toggle'}, show: function(event, ui) {$('.custom-select').selectbox();}, selected: 0});

                $('#sb-tabs-container').fadeIn('fast');
            });

        });

        return false;
    });

	$('.ad-box-holder .ad-preview-container').live('click', function() {
		$(this).next('.select-ad').trigger("click");
	});

    // Custom file
    $('.custom-upload').live('mouseenter', function() {
        $(this).addClass('custom-upload-hov');
    }).live('mousemove', function(e) {
        var fo = $('.custom-upload').offset();
        $(this).find('.file-upload').css({
            left : e.pageX - fo.left - 160,
            top : e.pageY - fo.top - 40
        });
    }).live('mouseleave', function() {
        $(this).removeClass('custom-upload-hov');
        if($(this).find('.file-upload').val() != '') {
            var va = $(this).find('.file-upload').val();
            $(this).parent().find('.blink').val(va);
        }
    });

    /*
    $('.tabs-content input[type="submit"]').live('click', function() {
        var indx = $(this).parents('.ui-tabs-panel:eq(0)').index()+ 1;
        var leng = $(this).parents('.tabs-content:eq(0) .ui-tabs-panel').length;
        if( indx < leng ) {
            $(this).parents('.ad-options').find('.tabs li:eq(' + indx + ') a').trigger('click');
        } else {
            $(this).parents('.ad-options').find('.tabs li:eq(0) a').trigger('click').parents('.ad-options:eq(0)').fadeOut();
        }
        return false;
    });
    */

   $('.sb-ads .file-upload').live('change', function(){
       if($(this).val()!=""){
           $(this).closest('form').submit();
       }
   });

   $('.sb-ads .custom-images img').live('click', function(){
       $('.sb-ads .custom-images img').each(function(){
           $(this).removeClass('selected');
       })
       $(this).addClass('selected');
       $(this).siblings('input').attr('checked', 'checked');
   });

    $('.lm-entry .cols .right .l-more').click(function() {
        $(this).parents('.cols:eq(0)').find('.older-newsletters').slideDown();
        return false;
    });

    $('.lm-entry .cols .right .l-more').toggle(function() {
        $(this).parents('.cols:eq(0)').find('.older-newsletters').slideDown();
        $(this).find('span').text('Less');
        return false;
    }, function() {
        $(this).parents('.cols:eq(0)').find('.older-newsletters').slideUp();
        $(this).find('span').text('More');
        return false;
    });

    // Awards boxes hover
    $('.the-awards > li').hover(function() {
    	$(this).addClass('aw-hov');
    	aw_timer = setInterval(function() {
    		$('.aw-hov .award-img img').animate({
    			height : 0,
    			opacity : 0
    		}, {
    			queue : false
    		});
    	}, 700);
    }, function() {
    	$('.aw-hov .award-img img').animate({
    		height : 167,
    		opacity : 1
    	}, {
    		queue : false
    	});
    	$(this).removeClass('aw-hov');
    	clearInterval(aw_timer);
    }).click(function() {
    	clearInterval(aw_timer);
    	if($('.aw-hov .award-img img').height() > 0) {
    		$('.aw-hov .award-img img').animate({
    			height: 0,
    			opacity : 0
    		}, {
    			queue : false
    		});
    	}
    	if($('.aw-hov .award-img img').height() == 0) {
    		$('.aw-hov .award-img img').animate({
    			height : 167,
    			opacity : 1
    		}, {
    			queue : false
    		});
    	}
    });

    // Team members hover
    $('.replaced').parent().hover(function() {
    	$(this).addClass('r-hover');

          $('.r-hover img').animate({
                   opacity : 0
          }, {
                   queue : false
          });

          $(this).find('.click-msg').slideToggle();

    }, function() {
    	$('.r-hover img').animate({
    		opacity : 1
    	}, {
    		queue : false
    	});
    	$('.r-hover').removeClass('r-hover');

         $(this).find('.click-msg').slideToggle();
    });

    // Press releases toggle
    $('.pr-list h6, .news-list h6').click(function() {
    	$(this).parent().find('p').slideToggle();
    });


    // Website Development Portfolio Image Viewer
    $('.case-fader .thumbs-holder a').mouseover(function() {
    	$('img', this).css({
    		width : 221,
    		height : 112,
    		marginTop : -5,
    		marginBottom : -5,
    		marginLeft : -10,
    		marginRight : -10
    	});
    }).mouseout(function() {
    	$('img', this).css({
    		width : 201,
    		height : 102,
    		margin : 0
    	});
    }).click(function() {
    	var href = $(this).attr('href');
    	$('.case-fader .image-holder img').fadeOut(function() {
    		$(this).attr('src', href).fadeIn();
    	});
    	$('.case-fader .thumbs-holder .act').removeClass('act');
    	$(this).addClass('act');
    	return false;
    });

    // Website Development Slider
    $('.logo-slider').jcarousel({
    	visible : 3,
    	scroll : 1,
    	wrap : "circular",
    	easing : "easeInOutSine",
    	animation : 500
    });

    $('.logo-slider .logo-holder, .logo-slider .logo-btn').click(function() {
    	document.location = $(this).parent().find('a[class*="btn"]').attr('href');
    	return false;
    });

    // Show all awards in Prospectors page
    $('.view-more-clients a').click(function() {
    	var aw = $(this).parents('.segment').find('.hidden-awards');
    	if(aw.is(':hidden')) {
    		$(this).text('Close Client Logos');
    		$('.hidden-awards').slideDown(800);
    	}else{
    		$(this).text('View More Clients');
    		$('.hidden-awards').slideUp(800);
    	}
    	return false;
    });


    // Init Prospectors sidebar caruosel
    $('.side-studies-carousel').jcarousel({
    	wrap : "circular",
    	visible : 1,
    	scroll : 1,
    	initCallback : sideSlider_initCallback,
    	buttonPrevHTML : null,
    	buttonNextHTML : null
    });


    // Add IE stylesheet
    if($.browser.msie) {
        $('head').append('<link rel="stylesheet" href="css/ie.css" type="text/css" media="all" />');
        if($.browser.version.substr(0,1) == 6) {
            DD_belatedPNG.fix('h1#logo a, .shell-b, .dd-m, .dd-b, #nav-agency .our-services, #nav-agency .our-services li, .btn-red1, .btn-red2, .btn-red3, .btn-red4, .btn-red5, .btn-red6, .btn-red7, .btn-red8, .btn-red9, .btn-red10, .btn-red11, .btn-red12, .btn-red13, #nav-contact .dd .ico-email, #nav-contact .dd .ico-phone, #nav-why .dd li a, #nav-why .dd li a span, #side-nav li a.active span, #side-nav ul ul a, .slider-container .jcarousel-next, .slider-container .jcarousel-prev, .slider-container li div, .btn-blue1, .blog-previews .entry-date p, .bot-col1, .bot-col2, .box .box-head, .box1 .box-t, .box1 .box-b, .box2 .box-t, .box2 .box-m, .box2 .box-b, .btn-chat, .btn-foot-contact, .btn-foot-phone, .btn-foot-chat, .btn-login, .case-studies .btn-blue1 .ico-pdf, .case-studies .study-logo .logo-wine, .header-call, .offer-entry ul li, .partner-logos .logo-bing, .partner-logos .logo-google, .partner-logos .logo-yahoo, .popup-l .interest, .popup-l .custom-checkbox, .popup-close, .row-order, .row-order p, .socials a, .step, .step .arrow, .studies-nav a, #nav-clients .dd .client-logos a, .dd img, .offer-entry .right img, .step img, .faq-close, .faq-row-head h5 span, .logo-facebook, .what-else .right img, .row-order span, .help, .box-packages .col-selected .col-t, .box-packages .col-selected .col-m, .box-packages .row-circ, .box-packages .ico-check, .ribbon-blue, .btn-red14, .btn-red15, .btn-red16, .btn-red17, .btn-red18, .btn-red19, .tooltip-t, .tooltip-b, .tooltip .ar, .ico-watch, .ico-team, .ico-security, .ico-nocontracts, .ico-awardwin, .ico-request, .ico-sendmsg, .ico-call, .ico-alert, .t-team, .ico-sendmail, .tabs li a span, .tabs li a em');
        }
    }

    // Add Mac stylesheet
    if ($.client.os == 'Mac') {
        $('head').append('<link rel="stylesheet" href="'+url('css/mac.css')+'" type="text/css" media="all" />');
    }

    $('#submit-another').live('click', function() {
       	$.facebox.close();
       	setTimeout(function() {
       		$('a#refer-friend').trigger('click');
       	}, 800);
        return false;
    });

	$('a.gotop-link').click(function(){
    	$('html, body').animate({
    		scrollTop: 0
    	}, function(){
    		$('.leadcapture .form input:first').focus();
    	});
    });

});

// Initialize custom selectboxes in popup
$(document).bind('reveal.facebox', function() {
    $('.custom-select').selectbox();
});

function pageLoaded()
{
	$('body').append('\
		<div class="tooltip">\
			<span class="ar"></span>\
			<div class="tooltip-t"></div>\
			<div class="tooltip-m">\
				<div class="cl">&nbsp;</div>\
				<div class="tooltip-i left"></div>\
				<div class="cl">&nbsp;</div>\
			</div>\
			<div class="tooltip-b"></div>\
			<div class="preload-images">\
				<img src="'+url('css/images/popup-b.png')+'" alt="" />\
				<img src="'+url('css/images/popup-m.png')+'" alt="" />\
				<img src="'+url('css/images/popup-t.png')+'" alt="" />\
				<img src="'+url('css/images/popup-s-b.png')+'" alt="" />\
				<img src="'+url('css/images/popup-s-m.png')+'" alt="" />\
				<img src="'+url('css/images/popup-s-t.png')+'" alt="" />\
				<img src="'+url('css/images/popup-close.png')+'" alt="" />\
				<img src="'+url('css/images/bg-award.png')+'" alt="" />\
				<img src="'+url('css/images/bg-award-first.png')+'" alt="" />\
				<img src="'+url('css/images/bg-award-last.png')+'" alt="" />\
				<img src="'+url('css/images/popup-l-t.png')+'" alt="" />\
				<img src="'+url('css/images/popup-l-m.png')+'" alt="" />\
				<img src="'+url('css/images/popup-l-b.png')+'" alt="" />\
			</div>\
		</div>\
	');

	if($('.replace-images').length)
	{
		$('.replace-images img').each(function(){
			var src = 'url(' + $(this).attr('src') + ')';
			$(this).wrap('<span class="replaced" />').parent().css({
				backgroundImage : src,
				backgroundPosition : 'bottom'
			});
		});
	}
}

// Login Help popups
function loginHelp() {
    $.ajax({
        url : 'ajax/login-help.html',
        success : function(data) {
            $('.popup-t h2').fadeOut('fast', function() {
                $(this).text('Login Help').fadeIn('fast');
            });
            $('.popup-i').slideUp(function() {
                $(this).html(data).slideDown();
            });
        }
    });
}

// Forgotten Password
function forgotPass() {
    $.ajax({
        url : 'ajax/forgot-pass.html',
        success : function(data) {
            $('.popup-t h2').fadeOut('fast', function() {
                $(this).text('Reset Password').fadeIn('fast');
            });
            $('.popup-i').slideUp(function() {
                $(this).html(data).slideDown();
            });
        }
    });
}

function backToLogin() {
    $.ajax({
        url : 'ajax/client-login2.html',
        success : function(data) {
            $('.popup-t h2').fadeOut('fast', function() {
                $(this).text('Client Login').fadeIn('fast');
            });
            $('.popup-i').slideUp(function() {
                $(this).html(data).slideDown();
            });
        }
    });
}

function url(url)
{
	if (url.indexOf('/') == 0)
	{
		url = url.substr(1);
	}
	return ($('#logo').attr('home')+url);
}

function dbg()
{
	var i, str, arg;

	str = '';
	for (i = 0; i < arguments.length; ++i)
	{
		arg = arguments[i];
		str += '('+arg+')';
	}
	$('body').append('<p>'+str+'</p>');
}

function currency(x)
{
	var i, c, cents;

	if (isNaN(x))
	{
		return '$0.00';
	}
	x = String(x);
	di = x.indexOf('.');
	if (di == -1)
	{
		x = x+'.00';
		di = x.length - 3;
	}
	else
	{
		cents = x.substr(di + 1);
		switch(cents.length)
		{
			case (0):
				cents += '0';
			case (1):
				cents += '0';
				break;
			default:
				cents = cents.substr(0, 2);
		}
		x = x.substr(0, di)+'.'+cents;
	}
	for (i = di - 3; i > 0; i -= 3)
	{
		x = x.substr(0, i)+','+x.substr(i);
	}
	return '$'+x;
}

function init_modules()
{
	if (!window.modules)
	{
		window.modules = {};
	}
}

function load_module(mod_name)
{
	//alert(mod_name);
	init_modules();
	
	if (!is_module_loaded(mod_name))
	{
		window.modules[mod_name] = new window[mod_name]();
	}
}

function is_module_loaded(mod_name)
{
	init_modules();
	return (typeof window.modules[mod_name] != 'undefined');
}


function find_dollars(text)
{
	var matches;

	matches = text.match(/\$(\d+\.\d+)/);
	if (matches && !isNaN(matches[1]))
	{
		return Number(matches[1]);
	}
	else
	{
		return 0;
	}
}

function createLogin() {
    $.ajax({
        url : 'ajax/create-login.html',
        success : function(data) {
            $('.popup-t h2').fadeOut('fast', function() {
                $(this).text('Create Login').fadeIn('fast');
            });
            $('.popup-i').slideUp(function() {
                $(this).html(data).slideDown();
            });
        }
    });
}

function ajaxResponse(response){
    var response_obj;
     try
     {
              response_obj = eval('('+response+')');
     }
     catch (err)
     {
              response_obj = response;
     }
     return response_obj;
}

/*
 * Wrapper function for Event Tracking in Google Analytics reporting.
 */
function trackEvent(category, action, label, value){
    if(typeof(_gaq)!='undefined' && _gaq.push){
        //should only exist for the live site
        _gaq.push(['_trackEvent', category, action, label, value]);
    }
}
