// DFS Homepage JavaScript

var currentPane = 1;
var autorate1 = 0;


function initHomepageTabs(){
	if(document.getElementById('homepage-tabs')){
		var tabs = document.getElementById('homepage-tabs');
		var li = tabs.getElementsByTagName('li');
		var tabCount = 0;
		var tabWidth = 0;
		var tabNumber = 0;
		while(li[tabCount]){
			if(li[tabCount].id != ''){
				li[tabCount].onmouseover = tabOver;
				li[tabCount].onmouseout = tabOut;
				li[tabCount].onclick = tabOn;
				li[tabCount].tabNum = tabNumber + 1;
				tabNumber++;
			}
			tabWidth += li[tabCount].offsetWidth;
			tabCount++;
		}
		var availablePixels = tabs.offsetWidth - tabWidth;
		var chunk = Math.floor(availablePixels/tabNumber);
		availablePixels = availablePixels % tabNumber;
		tabCount = 0;
		while(li[tabCount]){
			if(li[tabCount].id != ''){
				innerDivs = li[tabCount].childNodes;
				if(availablePixels){
					innerDivs[0].style.width = innerDivs[0].offsetWidth + chunk + 1 + 'px';
					availablePixels--;
				}
				else{
					innerDivs[0].style.width = innerDivs[0].offsetWidth + chunk + 'px';
				}
			}
			tabCount++;
		}
	}
	if(document.getElementById('tab-overlay')) {
		document.getElementById('tab-overlay').style.display ='none'; //fixes the width adjustment of the homepage tabs
	}
	if(currentPane == '5'){ 
	autorate1=1;
	callBackXml();
 }
}

function tabOver(){
	var nodeChildren;
	if(this.className != 'on'){
		nodeChildren = this.childNodes;
		nodeChildren[1].className = 'right-hover';
		this.className = 'hover';
	}
}
function tabOut(){
	var nodeChildren;
	if(this.className != 'on'){
		nodeChildren = this.childNodes;
		nodeChildren[1].className = 'right';
		this.className = '';
	}
}

var panh=true;
function tabOn(){
	
        if(panh==true){
	  panh=false;
          document.getElementById('pane'+currentPane).className = 'pane hide';
          
        }
	var nodeChildren;
	if(this.className != 'on'){
		var i = 0;
		var t = document.getElementById('homepage-tabs').getElementsByTagName('li');
		while(t[i]){
			if(t[i].id != ''){
				if((t[i].id != this.id) && (t[i].className == 'on')){
					t[i].className = '';
					t[i].firstChild.nextSibling.className = 'right';
				}
			}
			i++;
		}
		nodeChildren = this.childNodes;
		nodeChildren[1].className = 'right-on';
		this.className = 'on'; 
		if(document.getElementById('pane'+this.tabNum)){
			if(document.getElementById('pane'+currentPane)){
				document.getElementById('pane'+currentPane).className = 'pane hide';
				document.getElementById('pane'+this.tabNum).className = 'pane';
				currentPane = this.tabNum;
				
			}
		}
	}
	
if(currentPane == '5' && autorate1 == 0){ 
	autorate1=1;
	callBackXml();
 }
	return false;
}

var overlayArray = new Array();
overlayArray['security-popup'] = "<h3>Secure Account Center Log In</h3><p>Discover Card is serious about safeguarding your personal information online.</p><p>When you access your account and perform transactions on the Discover site we use 128-bit-Secure Sockets Layer (SSL) encryption technology—the most widely used method of securing internet transactions available today.</p>";
overlayArray['remember-userid-popup'] = "<h3>Remember Me</h3><p>Checking \"Remember Me \" will save your user ID on the computer you are using so you don't have to retype it every time you log in. To protect the security of your account, we can only save your user ID; we cannot save your account number.</p><ul><li>To turn off this feature, just uncheck the box and your user ID will not appear the next time you log in</li><li>Security Tip: when you save your user ID, it's stored on the computer you are using; therefore, you should never save your user ID on a public computer (e.g., in a library or airport).</li></ul>";
overlayArray['double-rewards-cashback'] = "<h3>Double Rewards</h3><p>Earn 2% <em>Cashback Bonus</em> on your first $1,000 in purchases made at grocery stores from November 1, 2008 until December 31, 2008. See important information on application for details.</p>";
overlayArray['double-rewards-promo'] = "<h3>Double Rewards</h3><p>Earn Double Miles on your first $1,000 in purchases made at grocery stores from November 1, 2008 until December 31, 2008. See important information on application for details.</p>";


function initOverlays(){
	var popups = document.getElementsByTagName('a');
	for(var i = 0; i < popups.length; i++){
		if(popups[i].className == 'question-popup'){
			popups[i].onclick = showOverlay;
		}
	}
	if(document.getElementById('homepage-overlay-close')){
		document.getElementById('homepage-overlay-close').onclick = function(){
			document.getElementById('homepage-overlay').style.display = 'none';
			return false;
		}
	}
}
function showOverlay(){
	if(document.getElementById('homepage-overlay')){
		var overlay = document.getElementById('homepage-overlay');
		var pos = findPos(this);
		var content = overlayArray[this.id];
		if(document.getElementById('overlay-content')){
			document.getElementById('overlay-content').innerHTML = content;
			overlay.style.left = pos[0] + 13 + 'px';
			overlay.style.top = pos[1] - 2 + 'px';
			overlay.style.display = 'block';
		}
	}
	return false;
}

 $(document).ready(function() {
	
	$(".giftcard-button img").hover(function() {
	  $(this).attr("src", "/images/giftcard-on.gif");
	}, function() {
	  $(this).attr("src", "/images/giftcard-off.gif");
	});
	
	$(".openaccount-button img").hover(function() {
	  $(this).attr("src", "/images/openaccount-on.gif");
	}, function() {
	  $(this).attr("src", "/images/openaccount-off.gif");
	});

	$(".wecanhelp-button img").hover(function() {
	  $(this).attr("src", "/images/wecanhelp-on.gif");
	}, function() {
	  $(this).attr("src", "/images/wecanhelp-off.gif");
	});
	
	$.ajax({
    	type: "GET",
        url: "/business/business-card/xml/rates.xml",
        dataType: "xml",
        success: function(xml) {
        	$(xml).find('type').each(function(){
            	var id = $(this).attr('id');
            	var schumer = $(this).attr('schumer');
				if (id == 'excellent' && schumer == 'A'){
					var intro_apr = $(this).find('purchases').find('intro').find('rate').text();
					$("#business-intro-apr")
					.html(intro_apr)
				}
            }); //close each(
		}
	});
	
	$.ajax({
    	type: "GET",
        url: "/business/business-miles-card/xml/rates.xml",
        dataType: "xml",
        success: function(xml) {
        	$(xml).find('type').each(function(){
            	var id = $(this).attr('id');
            	var schumer = $(this).attr('schumer');
				if (id == 'excellent' && schumer == 'A'){
					var intro_apr = $(this).find('purchases').find('intro').find('rate').text();
					$("#business-miles-intro-apr")
					.html(intro_apr)
				}
            }); //close each(
		}
	});
	
 });

addLoadEvent(initHomepageTabs);
addLoadEvent(initOverlays);
function callBackXml()
{
//alert("callBack - in ");
	$(document).ready(function() {
	//alert("ready");

        $.ajax({

        type: "GET",

        url: "assets/mm_rates.xml",

        dataType: "xml",

        success: function(xml) {

		

                                $(xml).find('rate').each(function() {

                                         var mma = $(this).attr('mma');
                                        var cd = $(this).attr('cd');
										var ira = $(this).attr('ira');
		
                                        $('#saving-headin1').html(mma);
                                        $('#saving-headin2').html(cd);
										$('#saving-headin3').html(ira);
                                }); //close each(

                }

        });

 });
}
