$(document).ready(function(){
	window.block_close = function(src){
		$('div[title=' + src + ']').trigger('click');
	};

    $(".toggle").each(function(n){
        var el = $(this);
        el.data('start_offset', {top: el.offset().top, left: el.offset().left});
    });

    var lst = $($($($('.toggle:last')[0]).closest('div.block')).next('a')[0]);
    if(lst){
        lst.css('margin-top', ($("#footer").offset().top + 40 - lst.offset().top) + 'px');
    }
	
	//Toggle Parent Block
	$(".toggle").click(function(){
		var toggle_state = ($(this).next("div").css("display") == "block")? 1 : 0;
		var parent_id = $(this).parent("div").attr("id");
		var iframe_src = ($(this).attr("title"))? $(this).attr("title") : "";
		//var notOpen = ($(this).next("div").html() == "")? true : false;

		//var myheight = $(window).height() + $(document).height();
		$(this).next("div").toggle();

		//Goto Parent Anchor on active block
		if(toggle_state == 0) {
			//alert("Parent Anchor");
			//window.location = "#"+parent_id;
			//$("#content").css("height", myheight+"px");
			$("#holder").css("display", "none");
			if(iframe_src) {
				$(this).next("div").html('<iframe src="'+ iframe_src + '" scrolling="no" frameborder="no" width="860" height="450">Your browser does not support inline frames</iframe>');
			}
		}
		//$("#holder").css("display", "none");
		//Clear height for Block
		if(toggle_state == 1) {
				// Close all Sub Panels
				$(this).next("div").children("div").each(function(){
				if ($(this).is('.open-block')) {
					$(this).removeClass("open-block");
				}
				$(this).children("div").css("display", "block");
				if ($(this).is('.slider')) {
					$(this).css("display", "none");
				}
			});

			$("#"+parent_id).css("height", "");
			//$("#content").css("height", "auto");

			if(iframe_src) {
				$(this).next("div").html('');
			}
		}

	});


	//Toggle Child Block
	$(".toggle-child").click(function(e){
		var trg = $(e.target);
		if(trg.attr('src') || trg.attr('href')){
			return;
		}
		
		//alert("Toggle Child Block");
		var counter = 0;
		var heightnew = 0;
		var toggle_state = ($(this).next("div").css("display") == "block")? 1 : 0;
		var iframe_src = ($(this).attr("title"))? $(this).attr("title") : "";
		
		//alert(iframe_src);
		//var notOpen = ($(this).next("div").html() == "")? true : false;
		//var parentid = ($(this).parent("div").parent("div").attr("id"))? $(this).parent("div").parent("div").attr("id") : "";

		if(toggle_state == 1) {
		// alert("1");
			$(this).removeClass("open-block");
			$(this).children("div").css("display", "block");
			$(this).children("a").css("display", "block");
			//$(this).parent("div").removeClass(parentid);
			//$(this).removeClass(parentid);

			if(iframe_src) {
				$(this).next("div").html('');
			}
		}
		else {
		//alert("2");
			$(this).addClass("open-block");
			$(this).children("div").css("display", "none");
			$(this).children("a").css("display", "none");
			//$(this).parent("div").addClass(parentid);
			//$(this).addClass(parentid);
			
			if(iframe_src) {
				$(this).next("div").html('<iframe src="'+ iframe_src + '" scrolling="no" frameborder="no" width="860" height="450">Your browser does not support inline frames</iframe>');
			}
		}

		$(this).next("div").toggle();

	});

	//$("a.anchor").anchorAnimate();
	$(".anchor").click(function(e){
        e.preventDefault();

		var id = $(this).attr('title');

        var is_scrolly = false;
        var is_scrollx = false;

        var el = $($("#"+id).children("div")[0]);
        var y = el.offset().top - $(document).scrollTop();
        var footer_y = $("#footer").offset().top - $(document).scrollTop();
        if(y < 0 || y > footer_y){
            is_scrolly = true;
        }

        var x = el.offset().left - $(document).scrollLeft();
        if(x < 0){
            is_scrollx = true;
        }

        if(is_scrolly){
            window.scrollBy(0, y - el.data('start_offset').top);
        }

        if(is_scrollx){
            window.scrollBy(x - el.data('start_offset').left, 0);
        }

        if(is_scrolly || is_scrollx)return;

		$($("#"+id).children("div")[0]).trigger('click');

		/*var iframe_src = ($("#"+id).children("div").attr("title"))? $("#"+id).children("div").attr("title") : "";
		//var notOpen = ($("#"+id).children("div").next("div").html() == "")? true : false;
		if($(this).is(".active")) {
			$("#"+id+" div.toggle").next("div").toggle();
		}
		else {
			if($("#"+id).is(':inView')) {
				 $("#"+id+" div.toggle").next("div").toggle();
				 move =false;
			}
			// Reset all active anchor
			$("#nav-bottom a").removeClass("active");

			// Set active anchor
			$(this).addClass("active");
		}
		
		if($("#"+id+" div.toggle").next("div").css("display") == "none") {
			if(iframe_src) {
				$("#"+id).children("div").next("div").html('<iframe src="'+ iframe_src + '" scrolling="no" frameborder="no" width="700" height="360">Your browser does not support inline frames</iframe>');
			}
		}
		else {
			if(iframe_src) {
				$("#"+id).children("div").next("div").html('');
			}
		}*/

	});
	 
	//Set Content Height
	var height_margin_space = 20;
	var height_browser = $(window).height();
	var height_footer = $("div#footer").height();
	var height_total = height_browser - height_footer - height_margin_space;
	$("div#content").css("height", height_total+"px");
	
	if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
		$("div#footer").css("position", "relative");
		$("div#footer").css("margin-top", "-1px");
		$("div#footer").css("padding-left", "131px");
	}
});

 
$.extend($.expr[':'],{
    inView: function(a) {        
    	var st = (document.documentElement.scrollTop || document.body.scrollTop),
          ot = $(a).offset().top,            
          wh = (window.innerHeight && window.innerHeight < $(window).height()) ? window.innerHeight : $(window).height();        
          return ot > st && ($(a).height() + ot) < (st + wh);    
        }
});
 
 



jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 0
	}, settings);	
	
	return this.each(function(){  
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href") 
 			var destination = $(elementClick).offset().top - 20;
			var id = $(caller).attr('title');
			var myEl =$("#"+id);
			if(elementClick=="#statement"){
				if($("#"+id+" div.toggle").next("div").css("display") == "none") {
					//$("#"+id).next("div").addClass("open-block");
					var temp1 = $("#"+id).children("div");
					var temp3 = temp1[1];
					$("#holder").css("display", "block");
					$("#"+id).next("div").html('<iframe src="Statement.jsp" scrolling="no" frameborder="no" width="860" height="450">Your browser does not support inline frames</iframe>');
				}else {
		 			$("#"+id).next("div").html('');
		 			$("#"+id).next("div").removeClass("open-block");
		 			$('div.slider').html('');
				}
				return true;
			} 
			$("#"+id).next("div").toggle();  
			var toggle_state = ($("#"+id).next("div").css("display") == "block")? 1 : 0;

			if($("#"+id).is(':inView')) {
				var parent_id = $("#"+id).parent("div").attr("id");
				$("#"+parent_id).css("height", "");
				$("#"+id).next("div").html('');
				
			}else{
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function(){});
			}
			myEl.find("div.slider").empty().hide(); 
			myEl.find("div").removeClass("open-block");
		  	return false;
		})
	})
}
 
