/* 
 * Capture the click event of all <a> tags with rel="modal" OR 
 * all <a> tags with an href urls containing request param "modal" 
 * with value "true" e.g. "/shop/warranties.jsp?modal=true"
 */ 
$(document).ready(function () {
	$('a').live('click', function (event) {
	    var url = $(this).attr('href');
	    var fromBack = $(this).attr('modalBack') ? true : false;
	    var disableClose = $(this).attr('disableModalClose') ? true : false;
	    var showScrollbars = $(this).attr('showscrollbars') ? true : false;
	    
	    if( $(this).attr('rel')=='modal' || jQuery.url.setUrl(url).param('modal')=='true' ){
		    // prevent default behaviour on click event
		    event.preventDefault(); 
		    
		    var modalCallBack = function (data) {
   		        openModalJQ(data,url,fromBack,disableClose, showScrollbars);
       		};
       		modalCallBack.hideUpdatingOverlay = true;
       		
			ajaxGet(this, modalCallBack);		
		}
	});
});

$(document).ready(function() {
    $('a[rel="ajaxSubmit"]').live('click', function (event) {
    	event.preventDefault();
    	
		var urlString = $(this).attr('href'); 	
    	if (urlString == null || urlString.length == 0) { 
			urlString = $(this).parents("form").attr('action');
		}
		
		performAjaxRequest(	urlString,
							eval($(this).attr('beforeSend')),
							eval($(this).attr('completeLoading')), 
							$(this).attr('dataType'),
							$(this).parents("form").serialize(), 
							eval($(this).attr('successLoading')), 
							eval($(this).attr('errorLoading')), 
							$(this).parents("form").attr('method'),
							getUniqueClass(this)
		);
	});
});

/*
 * Generate the dynamic tooltip elements.
 * Content is read from the static dropdown element, then edited to rename id attributes.
 * This avoids having elements with duplicate ids on the page.
 * Also added unique name element for each link, as this is required by LiveView.
 * Setting prerender to true enables the coremetrics script to attach events to the dynamic links at onload.
 */
$(document).ready(function () {
    $('#topNavigation .topNavItemWithMenu').each(function() {
        var target = $('.topnav', this);
        var content = $('.categoryDropNavContainer', this);

		var editedContent = content.clone();
		editedContent.find('a').attr('id', function() {
		  return 'DDM_'+this.id;
		});
		editedContent.find('a').attr('name', function() {
		  return this.id;
		});

        target.qtip({
            content: {
            	prerender: true,
                text: editedContent.html()
            },
            position: { 
                corner: getCorner(),
                adjust: {
                	screen: true,
                	scroll: false
                }
            },
            hide: { 
                when: 'mouseout', fixed: true 
            },
            style: {
                background: 'transparent',
                width: calculateWidth(),
                padding: 0,
                border: {
                    width: 0,
                    radius: 0
                }
            },
            api: {
                onShow: function() {
                    target.addClass('hovered');
                },
                onHide: function() {
                    target.removeClass('hovered');
                }
            },
            show : {
            	delay: 0
            }
        });
        
        function getCorner() {
            var leftPosition = target.offset().left - $('#topNavigation').offset().left;
            var maxLeftPosition = $('#topNavigation').width() - calculateWidth();  
            
            if (leftPosition > maxLeftPosition) {
                return { target: 'bottomRight', tooltip: 'topRight' };
            } else {
                return { target: 'bottomLeft', tooltip: 'topLeft' };
            }
        }
        
        function calculateWidth() {
            var width = getWidthWithMargin($('.refinementsColumn', content));
            width +=  getWidthWithMargin($('.promoColumnMiddle', content));
            width +=  getWidthWithMargin($('.promoColumnRight', content));
            width +=  getCssDimension($('.categoryDropNavContent', content),'border-left-width');
            width +=  getCssDimension($('.categoryDropNavContent', content),'border-right-width');
            width +=  getCssDimension($('.topNavSpacer1', content),'width');
            width +=  getCssDimension($('.topNavSpacer2', content),'width');
            width +=  getCssDimension($('.topNavSpacer3', content),'width');
 
            return width;
        }
        
        function getWidthWithMargin(element) {
            var width = getCssDimension(element,'width');
            width += getCssDimension(element,'margin-left');
            width += getCssDimension(element,'margin-right');
            
            return width;
        }
        
        function getCssDimension(element, cssClass) {
            if (element.css(cssClass)) {
                return parseInt(element.css(cssClass).replace('px', ''));
            }
            return 0;
        }     
    });
});


/*
 * The following functions need to run on each page load in order to maintain a user based 
 * set of footer logos. The logos below will be removed based on the customers account type,
 * and also whether 3d secure has been turned on or off.
 */

$(document).ready(function(){
	if (typeof removeCreditLogo != 'undefined') {
		if (removeCreditLogo == 'true') {
			$("div[id='personalAccount']").remove();
		}
	}
});
$(document).ready(function(){
	if (typeof display3DSecureFooterLogos != 'undefined') {
		if (display3DSecureFooterLogos == 'false') {
			$("img[title='Verified by Visa']").remove();
			$("img[title='MasterCard Securecode']").remove();
		}
	}
});	
