/* Dynamic Image Resizing */
$(window).load(function() {
        var theWindow        = $(window),
            $bg              = $("#bg"),
            aspectRatio      = $bg.width() / $bg.height();
        function resizeBg() {
                if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg
                        .removeClass()
                        .addClass('bgheight');
                } else {
                    $bg
                        .removeClass()
                        .addClass('bgwidth');
                }
        }
        theWindow.resize(function() {
                resizeBg();
        }).trigger("resize");

/* Implement Fading BG */
$("#bg").fadeIn(2000);
$("#quotes div").fadeIn(500);

/* Initiate Cycle */
    $("#quotes").cycle({
		pause: 3,
		speed:1500,
		fx: 'scrollRight', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		cssBefore:{
			opacity:1},
		animOut:{
			opacity:0},
		cleartypeNoBg: true,
		cleartype:true
	});
	
	$('img').bind('dragstart', function(event) { event.preventDefault(); });
});

$(document).ready(function() {

/* Show overlays on transaction thumbs */

$("#transactions > li").bind('mouseenter mouseleave', function() {
  $("div", this).toggle("fast");
});

/* Hide BG on load so we can implement Fading BG */
	$("#bg").css("display", "none");
	$("#quotes div").css("display", "none");

/* Implement Colorbox */
$(".privacy").colorbox({maxWidth:"80%", maxHeight:"80%", inline:true, href:"#privacytext"});
$(".business").colorbox({maxWidth:"80%", maxHeight:"80%", inline:true, href:"#businesstext"});


/* Initialize Scroll */
	$("#boxcontainer").jScrollPane({verticalDragMinHeight: 20,verticalDragMaxHeight: 20,showArrows: true, maintainPosition: false, autoReinitialise: true, hideFocus:true, hijackInternalLinks: true, animateScroll: true, animateEase: 'swing', animateDuration: 1000});

/* Implement Tabs */
	$("ul.tabs").tabs("div.panes > div", {
	history: true,
	onClick: function() {
		// "this" is a reference to the Tabs API
		var tabPanes = this.getPanes();
	}
	});
	
/* Set Indicator Starting Height */
var startingHeight = 220;

/* Dynamic Content Loading*/
    // Check for hash value in URL
    var hash = window.location.hash.substr(1);
    var href = $('#subnav li a').each(function(){
        var href = $(this).attr('href');
        if(hash==href.substr(0,href.length-0)){
            var toLoad = hash+' #boxtext';
            $('#boxtext').load(toLoad);
            $('#indicator').css(
			  { top: $('#'+hash).position().top+startingHeight }
			);
        }
		else{
			$('#indicator').css(
				{top:startingHeight});
			}
    });
    $('#subnav li a').click(function(event){
		var toLoad = $(this).attr('href')+' #boxtext';
		$('#boxtext').hide('fast',loadContent);
		$('#load').remove();
		$('#load').fadeIn('normal');
		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-0);
		function loadContent() {
			$('#boxtext').load(toLoad,'',showNewContent())
		}

		function showNewContent() {
			$('#boxtext').show('normal',hideLoader());
		}
		function hideLoader() {
			$('#load').fadeOut('normal');
		}

		var nav_item = $(event.target).parent('li');
		if (nav_item.position()!= undefined) {

			// Change style on clicked item and unclicked item
			$('#subnav li').removeClass('current');
			$(nav_item).addClass('current');

			// Animate indicator cursor
			$('#indicator').animate(
			  { top: $(nav_item).position().top+startingHeight},
			  { duration: 500, easing: 'easeOutBack' }
			);
		}
		return false;
	});

	/* Dynamic Content Loading 1*/
    // Check for hash value in URL
    var hash = window.location.hash.substr(1);
    var href = $('#subnav1 li a').each(function(){
        var href = $(this).attr('href');
        if(hash==href.substr(0,href.length-0)){
            var toLoad = hash+' #boxtext1';
            $('#boxtext1').load(toLoad);
            $('#indicator1').css(
			  { top: $('#'+hash).position().top+startingHeight }
			);
        }
		else{
			$('#indicator1').css(
				{top:startingHeight});
			}
    });
    $('#subnav1 li a').click(function(event){
		var toLoad = $(this).attr('href')+' #boxtext1';
		$('#boxtext1').hide('fast',loadContent);
		$('#load1').remove();
		$('#load1').fadeIn('normal');
		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-0);
		function loadContent() {
			$('#boxtext1').load(toLoad,'',showNewContent())
		}

		function showNewContent() {
			$('#boxtext1').show('normal',hideLoader());
		}
		function hideLoader() {
			$('#load1').fadeOut('normal');
		}

		var nav_item = $(event.target).parent('li');
		if (nav_item.position()!= undefined) {

			// Change style on clicked item and unclicked item
			$('#subnav1 li').removeClass('current');
			$(nav_item).addClass('current');

			// Animate indicator cursor
			$('#indicator1').animate(
			  { top: $(nav_item).position().top+startingHeight},
			  { duration: 500, easing: 'easeOutBack' }
			);
		}
		return false;
	});

	/* Dynamic Content Loading 2*/
    // Check for hash value in URL
    var hash = window.location.hash.substr(1);
    var href = $('#subnav2 li a').each(function(){
        var href = $(this).attr('href');
        if(hash==href.substr(0,href.length-0)){
            var toLoad = hash+' #boxtext2';
            $('#boxtext2').load(toLoad);
            $('#indicator2').css(
			  { top: $('#'+hash).position().top+startingHeight }
			);
        }
		else{
			$('#indicator2').css(
				{top:startingHeight});
			}
    });
    $('#subnav2 li a').click(function(event){
		var toLoad = $(this).attr('href')+' #boxtext2';
		$('#boxtext2').hide('fast',loadContent);
		$('#load2').remove();
		$('#load2').fadeIn('normal');
		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-0);
		function loadContent() {
			$('#boxtext2').load(toLoad,'',showNewContent())
		}

		function showNewContent() {
			$('#boxtext2').show('normal',hideLoader());
		}
		function hideLoader() {
			$('#load2').fadeOut('normal');
		}

		var nav_item = $(event.target).parent('li');
		if (nav_item.position()!= undefined) {

			// Change style on clicked item and unclicked item
			$('#subnav2 li').removeClass('current');
			$(nav_item).addClass('current');

			// Animate indicator cursor
			$('#indicator2').animate(
			  { top: $(nav_item).position().top+startingHeight},
			  { duration: 500, easing: 'easeOutBack' }
			);
		}
		return false;
	});
});
