﻿jQuery.noConflict();
jQuery(function($) {

    try {
        var tracker = eConduit.createEventTracker();
    }
    catch (error) {
        // there was an error setting up eConduit
    }

    $('#arcbox a').click(function() {
        var parentCls = $(this).parent().attr('class');
        var parentID = $(this).parent().attr('id');
        if (parentCls != 'view-demo' && parentID != 'buying-guides' && parentID != 'shopping-checklists') {
            var theTag = $(this).attr('rev');
            if (theTag) { tracker.trackEvent(theTag); }
        }
    });
    $('#buying-guides a').click(function() {
        var theTag = $('#buying-guides select :selected').attr('id');
        var theURL = $('#buying-guides select').val();
        if (theURL) {
            window.location.assign(theURL);
            if (theTag) { tracker.trackEvent(theTag); }
        }
        return false;
    });
    $('#shopping-checklists a').click(function() {
        var theTag = $('#shopping-checklists select :selected').attr('id');
        var theURL = $('#shopping-checklists select').val();
        if (theURL) {
            window.location.assign(theURL);
            if (theTag) { tracker.trackEvent(theTag); }
        }
        return false;
    });
    $('#arcbox-site-nav #offers-flash object').css('cursor', 'pointer').click(function() {
        var theURL = $('#arcbox-site-nav #offers-link a').attr('href');
        window.location.assign(theURL);
        return false;
    });

    $('.view-demo a, .view-demo-text a').click(function(e) {
        e.preventDefault();
        var theTag = $(this).attr('rev');
        var demoURL = $(this).attr('href');
        var demoPath = /[^\s]+(?=\.html)/;
        var demoFile = demoURL.replace(demoPath, '');
        if (demoFile == '.html') { window.open(demoURL, '_blank', 'height=771,width=920'); }
        else { window.open(demoURL, '_blank', 'height=511,width=629'); }
        if (theTag) { tracker.trackEvent(theTag); }
    });
    $('.arcnodemo').hide();

    $('.print-coupon a').click(function(e) {
        e.preventDefault;

        var href = $(this).attr('href');

        window.open(href, '_blank', 'status = 0, toolbar = 0, location = 0, menubar = 0, resizable = 0, height = 600, width = 700');

        return false;
    });

    addThis();
    globalTabs();
    wpFloodlightTags();
});

function addThis() {
    /* ADD THIS */
    var addthis_pub = "arclowes";
    $('#arcbox-bookmark-this a.addthisButton').click(function() {
        return addthis_sendto();
    });
    $('#arcbox-bookmark-this a.addthisButton').mouseout(function() {
        addthis_close();
    });
    $('#arcbox-bookmark-this a.addthisButton').mouseover(function() {
        return addthis_open(this, '', '[URL]', '[TITLE]');
    });
}

function splitURL()
{
    var url = document.location.href;
    var urlsplit = new Array();
    
    urlsplit = url.split('#');
    var query = urlsplit[1];
    
    if(query != null || query != undefined)
    {
        switch(query)
        {
            case 'cabrio':
                jQuery('#duet').hide();
                jQuery('#cabrio').show();
                break;
            
            case 'duet':
                jQuery('#cabrio').hide();
                jQuery('#duet').show();
                break;
            
            case 'resourcesaver':
                jQuery('.togglepromos > .togglepromotabs > li a').each(function()
                {
                    jQuery(this).removeClass('active');
                })
                
                jQuery('.togglepromos > .secondarypromo').hide();
                jQuery('.togglepromos > .primarypromo').show();
                
                jQuery('.togglepromotabs > .primarypromo > a').addClass('active');
                break;
            
            case 'latitude':
                jQuery('.togglepromos > .togglepromotabs > li a').each(function()
                {
                    jQuery(this).removeClass('active');
                })
                
                jQuery('.togglepromos > .primarypromo').hide();
                jQuery('.togglepromos > .secondarypromo').show();
                
                jQuery('.togglepromotabs > .secondarypromo > a').addClass('active');
                break;
        }
    }
}

// DoubleClick Floodlight tracking hack since Floodlight was not built
// to track click actions to offsite pages or PDFs.
function wpFloodlightTags() {
    jQuery("a.floodlight").click(function(e) {
        e.preventDefault();
        var href = $(this).attr("href");
        var cat = $(this).attr("rel");
        var t = $(this).attr("target");
        var url = "http://fls.doubleclick.net/activityi;src=2625291;type=whirllp;cat=" + cat + ";ord=" + ((Math.random() + "") * 10000000000000);
        jQuery("#flFrame").attr("src", url).one("load", function() {
            if (t == "_blank") {
               window.open(href);
            } else {
                self.location = href;
            }
        });
    });
}

function globalTabs() {
    /* Initialize tabs */
    jQuery('#home-top-left').tabs({
        show: function(event, ui) {
            globalTabTitles(); 
        }
    }).tabs('rotate', 5000);
    jQuery('#home-top-right').tabs({
        show: function(event, ui) {
            globalTabTitles();
        }
    }).tabs('rotate', 5000);
    jQuery('#kitchen-main').tabs({
        show: function(event, ui) {
            globalTabTitles();
        }
    }).tabs('rotate', 5000);
    jQuery('#laundry-main').tabs({
        show: function(event, ui) {
            globalTabTitles();
        }
    }).tabs('rotate', 5000);

    /* Set titles on page load */
    globalTabTitles();
    
    $(".global-tabs a").click(function() {
        var title = $(this).attr("title");
        $(this).closest("li").siblings('.link-title').html(title);
    });
}

function globalTabTitles() {
    $('.global-tabs li.link-title').each(function() {
        var currentTitle = $(this).parent().children('.ui-tabs-selected').find('a').attr("title");
        $(this).html(currentTitle);
    });
}