/*===========================================================================
	Behavior (Global)
	
	This JavaScript executes dynamic behaviors such as:
	* Preloading images
	* Flash Replacements (i.e. sIFR, UFO, SWFObject, etc.)
	* Decorate the DOM with presentational elements
	* Collapsing/expanding sections
	* Form validation
	* Popup windows
===========================================================================*/

/* Enable FireBug/etc. logging if logging widget is loaded
===========================================================================*/

if (window.YAHOO && YAHOO.widget.Logger) {
	YAHOO.widget.Logger.enableBrowserConsole();
}


/* Expand/collapse Log In form on interior pages
===========================================================================*/

jQuery.noConflict();
jQuery(document).ready(function() {
	
	//Remote window check
	var sidebarIsHidden = jQuery("div.sidebar").is(':hidden');
	/*
	if(sidebarIsHidden){
		jQuery("a").removeAttr("href");
		jQuery("a").removeAttr("title");
	}
	*/
	//bind click for demo button in account-resources/internet-banking/
	jQuery("a#retailDemo").click(function(){
		if (window.confirm("You are submitting information to an external page.\nAre you sure?")) {
			var f = document.createElement('form');
			f.style.display = 'none'; this.parentNode.appendChild(f);
			var h = document.createElement('input'); h.type = 'hidden'; h.name = 'demo'; h.value = 'retail'; f.appendChild(h);
			var h2 = document.createElement('input'); h2.type = 'hidden'; h2.name = 'demoretailbutton'; h2.value = 'Retail Demo'; f.appendChild(h2);
			f.name = 'retaildemo';
			f.method = 'POST';
			f.target = '_blank';
			f.action = 'https://ibank.plainscapital.com/PlainsCapitalOnline/Authentication/Login.aspx';
			f.submit();
			return false;
		}
	});

	//bind click for demo button in account-resources/internet-banking/
	jQuery("a#commercialDemo").click(function() {
		if (window.confirm("You are submitting information to an external page.\nAre you sure?")) {
			var f = document.createElement('form');
			f.style.display = 'none'; this.parentNode.appendChild(f);
			var h = document.createElement('input'); h.type = 'hidden'; h.name = 'demo'; h.value = 'commercial'; f.appendChild(h);
			var h2 = document.createElement('input'); h2.type = 'hidden'; h2.name = 'democommercialbutton'; h2.value = 'Commercial Demo'; f.appendChild(h2);
			f.name = 'retaildemo';
			f.method = 'POST';
			f.target = '_blank';
			f.action = 'https://ibank.plainscapital.com/PlainsCapitalOnline/Authentication/Login.aspx';
			f.submit();
			return false;
		}
	});
	
	//write secure pop up
	var popUp = '<div class="loginSecurePop" style="display: none;"><a href="#" class="loginSecurePopCloseWindow">Close Window</a><div class="loginSecurePopBody"><h1>Online Security</h1><h2>Your Online Security is Our Priority</h2><p>When you sign on to PlainsCapital Internet Banking, your username and password are secure. We use 128-bit Secure Sockets Layer (SSL) encryption. This means that <u>only</u> PlainsCapital Bank has access to your username and password.</p><h2>PlainsCapital is committed to protecting your information</h2><ul><li><strong>128-bit Secure Sockets Layer (SSL) technology.</strong> To access your account, you must use a browser that supports 128-bit encryption-the most widely used method of securing internet transactions available today.</li><li><strong>Online username and password.</strong> Never share your username and password with anyone. PlainsCapital Bank will never ask you for your username and password when servicing your account.</li><li><strong>Timeouts.</strong> PlainsCapital automatically logs you out of Internet Banking after 45 minutes of inactivity.</li><li><strong>Firewalls.</strong> The server that hosts PlainsCapital\'s Internet Banking application is protected by a firewall. This prevents unauthorized access to our network. We constantly monitor our server to prevent security breaches.</li></ul></div></div>';
	var interiorLock = '<div class="loginSecurePopWelcomeLabel">Welcome to Internet Banking</div><a href="#" class="loginSecure"><img src="/Style%20Library/_media/_global/interior_page_secure_lock.gif" height="20" width="20" /></a>';
	var clearLock = '<a href="#" class="loginSecure"><img src="/Style%20Library/_behavior/clear.gif" height="20" width="20" /></a>';
	var privateBankingLock = '<a href="#" class="loginSecure"><img src="/Style%20Library/_media/_global/interior_page_secure_lock_white.gif" height="20" width="20" /></a>';
	jQuery("body.personal-banking.home-page .log-in-wrapper").append(clearLock);
	jQuery("body.commercial-banking.home-page .log-in-wrapper").append(clearLock);
	jQuery("body.personal-banking.interior-page .log-in-wrapper h3").after(interiorLock);
	jQuery("body.commercial-banking.interior-page .log-in-wrapper h3").after(interiorLock);
	jQuery("body.private-banking.interior-page .log-in-wrapper h3").append(privateBankingLock);
	jQuery("body.private-banking.home-page .log-in-wrapper h3").append(privateBankingLock);
	jQuery("body.market-page .log-in-wrapper .log-in-form").prepend(interiorLock);
	jQuery("body.search-results .log-in-wrapper .log-in-form").prepend(interiorLock);

	jQuery("body div.content-and-sidebar-wrapper").append(popUp);

	// default to hidden on interior pages
	jQuery("body:not(.home-page)").find((".log-in-wrapper > *:not(h3, div.bottom-corners)")).hide();
	
	// toggle visibility
	jQuery("body .log-in-wrapper a.loginSecure").click(function() {
		jQuery("body div.content-and-sidebar-wrapper div.loginSecurePop").toggle("slow");
		return false;
	});
	jQuery("body div.content-and-sidebar-wrapper div.loginSecurePop a.loginSecurePopCloseWindow").click(function() {
		jQuery("body div.content-and-sidebar-wrapper div.loginSecurePop").hide();
		return false;
	});

	
	// toggle visibility
	jQuery(".log-in-wrapper > h3 > a.logInTrigger").click(function() {
		jQuery(".log-in-wrapper > *:not(h3, div.bottom-corners)").slideToggle("slow");
		return false;
	});
	
	// toggle arrow direction
	jQuery(".log-in-wrapper > h3 > a.logInTrigger").toggle(function() {
		jQuery("body.personal-banking.interior-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-down_white.gif)");
		jQuery("body.commercial-banking.interior-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-down_white.gif)");
		jQuery("body.market-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-down_white.gif)");
		jQuery("body.market-page.home-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-right_white.gif)");
		jQuery("body.search-results .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-down_white.gif)");
	}, function() {
		jQuery("body.personal-banking.interior-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-right_white.gif)");
		jQuery("body.commercial-banking.interior-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-right_white.gif)");
		jQuery("body.market-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-right_white.gif)");
		jQuery("body.market-page.home-page .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-down_white.gif)");
		jQuery("body.search-results .log-in-wrapper > h3").css("background-image","url(/Style%20Library/_media/bullets/arrow-right_white.gif)");
	});
	
	/*
	* jQuery rules to globally adjust the left nav
	*/
	jQuery("table#zz1_GlobalNav table table a").css("padding-left","16px");
	jQuery("table#zz1_GlobalNav table td").css("width","215px");

	if(jQuery("body").find(".leftNav3") && !jQuery("body").find(".leftNav4")){
		jQuery("table.leftNav3").hide();
		jQuery("table.leftNav3").parent().hide();
		jQuery("table.leftNav3").parent().parent().hide();
		jQuery("table.leftNav3").parent().parent().parent().hide();
		jQuery("table.leftNav3").parent().parent().parent().parent().hide();
		jQuery("table.leftNav3").parent().parent().parent().parent().parent().hide();
		jQuery("table.leftNav3").parent().parent().parent().parent().parent().parent().hide();
	}
	jQuery("table.leftNav4").hide();
	jQuery("table.leftNav4").parent().hide();
	jQuery("table.leftNav4").parent().parent().hide();
	jQuery("table.leftNav4").parent().parent().parent().hide();
	jQuery("table.leftNav4").parent().parent().parent().parent().hide();
	jQuery("table.leftNav4").parent().parent().parent().parent().parent().hide();
	jQuery("table.leftNav4").parent().parent().parent().parent().parent().parent().hide();

	jQuery("body.private-banking table.selectedHover").parent().css("background-color","#6c5d53");
	jQuery("body.private-banking table.leftNav3").parent().css("background-color","#6c5d53");
	jQuery("body.private-banking td a.selectedHover").css("color","#fbe4ae");
	
	/* Commercial interior */
	jQuery("body.commercial-banking table.leftNav2 td").css("background-color","#8A3434");
	jQuery("body.commercial-banking table.leftNav3").hide();
	jQuery("body.commercial-banking table.leftNav3").parent().hide();
	jQuery("body.commercial-banking table.leftNav3").parent().parent().hide();
	jQuery("body.commercial-banking table.leftNav3").parent().parent().parent().hide();
	jQuery("body.commercial-banking table.leftNav3").parent().parent().parent().parent().hide();
	jQuery("body.commercial-banking table.leftNav3").parent().parent().parent().parent().parent().hide();
	jQuery("body.commercial-banking table.leftNav3").parent().parent().parent().parent().parent().parent().hide();

	/* About Section */
	jQuery("body.about-us div.page table.secondaryNav table.secondaryNavSelected").parent().css("background-position","0 -27px");

	var url = document.location.toString();
		if (url.match('major-markets/greater-dallas') || url.match('MAJOR-MARKETS/GREATER-DALLAS')) {
			jQuery("body.greater-austin div.page").css("background-image","url(/SiteCollectionImages/bg_market_dallas.jpg)");
		}
		if (url.match('major-markets/greater-fort-worth') || url.match('MAJOR-MARKETS/GREATER-FORT-WORTH')) {
			jQuery("body.greater-austin div.page").css("background-image","url(/SiteCollectionImages/bg_market_ft-worth.jpg)");
		}
		if (url.match('major-markets/lubbock') || url.match('MAJOR-MARKETS/LUBBOCK')) {
			jQuery("body.greater-austin div.page").css("background-image","url(/SiteCollectionImages/bg_market_lubbock.jpg)");
		}
		if (url.match('major-markets/greater-austin') || url.match('MAJOR-MARKETS/GREATER-AUSTIN')) {
			jQuery("body.greater-austin div.page").css("background-image","url(/SiteCollectionImages/bg_market_austin.jpg)");
		}
		if (url.match('major-markets/san-antonio') || url.match('MAJOR-MARKETS/SAN-ANTONIO')) {
			jQuery("body.greater-austin div.page").css("background-image","url(/SiteCollectionImages/bg_market_sanantonio.jpg)");
		}
	
	ieupdate();
	
	jQuery('div.search-form input[type=text]').bind('keypress', function(e) { if (e.which == 13) { e.preventDefault; return false; } });


	/* Page Tags */
	jQuery('div.pageTags div.ms-formfieldcontainer').parent().css('display','block');
	
	/* sign-up form */
/*
	jQuery('td#MSOZoneCell_WebPartWPQ5 table td:first-child').css('width','30%');
	jQuery('td#MSOZoneCell_WebPartWPQ5 table td:last-child').css('width','70%');
*/
	jQuery('div.log-in-form fieldset p').css('position','relative');
});


function openpopup(url, height, width) {
	this.newwindow = window.open(url,'name','height=' + height + ',width=' + width);
	if (window.focus) {this.newwindow.focus()}
	return false;
}

function ieupdate(){
	var strBrowser = navigator.userAgent.toLowerCase();
	
	if(strBrowser.indexOf("msie") > -1 && strBrowser.indexOf("mac") < 0){
		var theObjects = document.getElementsByTagName('object');
		var theObjectsLen = theObjects.length;
		for (var i = 0; i < theObjectsLen; i++) {
			if(theObjects[i].outerHTML){
				if(theObjects[i].data){
					theObjects[i].removeAttribute('data');
				}
				var theParams = theObjects[i].getElementsByTagName("param");
				var theParamsLength = theParams.length;
				for (var j = 0; j < theParamsLength; j++) {
					if(theParams[j].name.toLowerCase() == 'flashvars'){
						var theFlashVars = theParams[j].value;
					}
				}
				var theOuterHTML = theObjects[i].outerHTML;
				var re = /<param name="FlashVars" value="">/ig;
				theOuterHTML = theOuterHTML.replace(re,"<param name='FlashVars' value='" + theFlashVars + "'>");
				theObjects[i].outerHTML = theOuterHTML;
			}
		}
	}
}

window.onunload = function() {
	if (document.getElementsByTagName) {
		var objs = document.getElementsByTagName("object");
		for (i=0; i<objs.length; i++) {
			objs[i].outerHTML = "";
		}
	}
}

var slideshow = {
            
	current_item  : 0,
	total_items   : 0,
	timer_running : false,
	queued_requests : 0,
	
	init : function() {
	    if (jQuery('#banner_slideshow_container').length == 0) return false;
	    
	    slideshow.current_item = jQuery('li.current_slideshow_image').attr('id').replace('slideshow_image_','');
	    slideshow.total_items = jQuery('li.slideshow_image').length;
	    
	    jQuery('.slideshow_image').hide();
	    jQuery('li.current_slideshow_image').show();
	
		jQuery('ul#banner_slideshow_pickers li').click(function() {
		    slideshow.timer_running = false;
		    if (jQuery(this).attr('id').replace('slideshow_image_picker_','') != slideshow.current_item) {
		        slideshow.current_item = jQuery(this).attr('id').replace('slideshow_image_picker_','');
		        slideshow.set_active_image();
		        if (jQuery('#slideshow_play').hasClass('playing')) slideshow.start_slideshow();
		    }
		});
		
		jQuery('#slideshow_play').click( function() {
		    if (jQuery('#slideshow_play').hasClass('playing')) { slideshow.stop_slideshow(); jQuery('#slideshow_play').text('play').removeClass('playing'); }
		    else { slideshow.start_slideshow(); jQuery('#slideshow_play').text('pause').addClass('playing'); }
		});
		
		jQuery('#slideshow_previous').click( function() {
		    slideshow.stop_slideshow();
		    
		    if (slideshow.current_item == 1) { slideshow.current_item = slideshow.total_items; }
		    else { slideshow.current_item = (slideshow.current_item / 1) - 1; }
		    
		    slideshow.set_active_image();
		    if (jQuery('#slideshow_play').hasClass('playing')) slideshow.start_slideshow();
		});
		
		jQuery('#slideshow_next').click( function() {
		    slideshow.stop_slideshow();
		    
		    if (slideshow.current_item == slideshow.total_items) { slideshow.current_item = 1; }
		    else { slideshow.current_item = (slideshow.current_item / 1) + 1; }
		    
		    slideshow.set_active_image();
		    if (jQuery('#slideshow_play').hasClass('playing')) slideshow.start_slideshow();
		});
		
		slideshow.timer_running = true;
	},
	
	set_active_image : function() {
	    jQuery('.slideshow_image').fadeOut('slow').removeClass('current_slideshow_image');
        jQuery('#slideshow_image_' + slideshow.current_item).fadeIn('slow').addClass('current_slideshow_image');
        
        jQuery('ul#banner_slideshow_pickers li').removeClass('current_slideshow_image_picker');
		jQuery('#slideshow_image_picker_' + slideshow.current_item).addClass('current_slideshow_image_picker');
	},
	
	advance_slideshow : function() {
		if (!slideshow.timer_running) { slideshow.queued_requests--; return false; }

        if (slideshow.current_item == slideshow.total_items) { slideshow.current_item = 1; }
		else { slideshow.current_item = (slideshow.current_item / 1) + 1; }
		    
		slideshow.set_active_image();
		slideshow.waiting_to_start = false;
		setTimeout(slideshow.advance_slideshow, 10000);
	},
	
	start_slideshow : function() {
        slideshow.queued_requests++;

	    setTimeout( function() {
	        if (slideshow.queued_requests > 1) {
	            slideshow.queued_requests--;
	        }
	        else if (jQuery('#slideshow_play').hasClass('playing')) {
	            slideshow.timer_running = true;
	            slideshow.advance_slideshow();
	        }
	    }, 10000);
	},
	
	stop_slideshow : function() {
	    slideshow.timer_running = false;
	}

};

jQuery(document).ready(function() {
	
	slideshow.init();
	
	jQuery('select#open-an-account').change(function(){
		if (this.options[this.selectedIndex].value.length > 0)
			window.open(this.options[this.selectedIndex].value);
        
        return false;
    });
	
});

jQuery(window).load(function() { setTimeout(slideshow.start_slideshow, 5000); });