$(document).ready(function() {
    init_tooltips();
    update_iframe();

    $('.photo_gallery ul li a').lightBox();
    $('#provider_photos_small ul li a').lightBox();
    $('.cancel_waitlist_mode').click(function() {
        $.ajax({
            'url': '/shoppingcart/ajax/waitlist_mode/off/',
            'type': 'POST',
            success: function() {
                window.location.reload();
            }
        });
    });
    
    $('div#slides #get_started_button').click(function() {
        window.location = '/registration/';
    });
    
    $('#site_navigation li ul.dropnav').hide();
    $('#site_navigation li').hover(function () {
        $('.dropnav',this).fadeIn("fast");
    },
    function () {
        $('.dropnav',this).fadeOut("fast");
    });
    
    $('#site_navigation li.active').hover(  function () {
        $('.dropnav',this).fadeIn("fast");
    },
    function () {
        $('.dropnav',this).fadeOut("fast");
    });
    
    
    $('#print, #print_button').click(function() {
       window.print();
       return false;
    });
    
    function check_popups()
    {
        if($(window).width() < $('#page').width() + $('#site_navigation li a.last').next().width()) {
            $('#site_navigation li a.last').next().addClass('left_pop');
        } else {
            $('#site_navigation li a.last').next().removeClass('left_pop');
        }
    }
    $(window).resize(function() {
        check_popups();
    });
    check_popups();
});

function init_tooltips() {
    $('#credit_card_has_pending_holds').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-150, width:200, height: 135, cluetipClass:'activity_tree', dropShadowSteps:0});
    
    $('.popup_small').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-215, topOffset:-60, width:200, height: 45, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.popup_tiny').cluetip({local:true, showTitle: false, arrows:false, positionBy:'fixed', leftOffset:0, topOffset:-55, width:120, height: 40, cluetipClass:'activity_tree', dropShadowSteps:0});

    $('.popup_medium').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-215, topOffset:-70, width:200, height: 55, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.help').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-115, width:200, height: 100, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.help_large').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-392, topOffset:-165, width:400, height: 150, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.popup_large').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-215, topOffset:-115, width:200, height: 100, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.installment_plan_info').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-442, topOffset:-265, width:450, height: 250, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.cancellation_policy').cluetip({local:true, showTitle: false, arrows:false, positionBy:'fixed', leftOffset:0, topOffset:-205, width:300, height: 190, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.registration_policy').cluetip({local:true, showTitle: false, arrows:false, positionBy:'fixed', leftOffset:0, topOffset:-160, width:300, height: 145, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.equipment_policy').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-292, topOffset:-160, width:300, height: 145, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.alert').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-95, width:200, height: 80, cluetipClass:'activity_tree', dropShadowSteps:0});
    
    $('.pending_approval').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-205, width:200, height:190, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.pending_payment').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-110, width:200, height: 95, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.pending_assessment').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-205, width:200, height: 190, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.pending_membership').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-160, width:200, height: 145, cluetipClass:'activity_tree', dropShadowSteps:0});
    $('.maximum_price').cluetip({local:true, showTitle: false, arrows:true, positionBy:'fixed', leftOffset:-192, topOffset:-225, width:200, height: 210, cluetipClass:'activity_tree', dropShadowSteps:0});
    
}

function update_iframe() {
    var query_string = window.location.search;
    query_string = query_string.replace("?", "");
    var params = query_string.split("&");
    
    if (!window.parent) {
        $.post('/customer/ajax/update-framing/', { framed: 'False' }, function(data) {
            if (data == 'reload') {
                if ($.inArray("framed=True", params) > -1) {
                    params[$.inArray("framed=True", params)] = "";
                    params = "?" + params.join("&");
                    var href = window.location.protocol + "//";
                    href += window.location.host;
                    href += window.location.pathname;
                    href += params;
                    window.location.assign(href);
                } else {
                    window.location.reload();
                }
            }
        });
    } else if (window.parent.frames.length == 0) {
        $.post('/customer/ajax/update-framing/', { framed: 'False' }, function(data) {
            if (data == 'reload') {
                if ($.inArray("framed=True", params) > -1) {
                    params[$.inArray("framed=True", params)] = "";
                    params = "?" + params.join("&");
                    var href = window.location.protocol + "//";
                    href += window.location.host;
                    href += window.location.pathname;
                    href += params;
                    window.location.assign(href);
                } else {
                    window.location.reload();
                }
            }
        });
    }
}

function build_url(url, params) {
    new_url = url + '?';
    for (param in params) {
	new_url += param + '=' + params[param] + '&';
    }
    return new_url.slice(0, new_url.length-1);
}

function update_analytics(url) {
    try {
	__gaq.push(['_trackPageview', url]);
    } catch (err) {}
}