﻿var g_fadebox_added = 0;


$(window).resize(function() { //moved 3.28.2011 to main$(window).resize
	if (g_fadebox_added == 1) { 
		resize_fadebox();
		move_encase();
	}
});
// --------------------------------------------------------------------------------------------------------------------------------------------------------------
//ENCASE FUNCTIONS
//todo change fadeBox to id = faster searching for it - should only ever be on DOM once

function encase(url,w,h) {
    //$(document).scrollTop(0);//scroll to the top of the document
	show_fadebox();
	 
	var wpx = (w)? w : 680;
	var hpx = (h)? h : 450;
	
	dlog(hpx );
	$('<div id="encase" class="encase"><div class="encase_content_wrapper"><div class="encase_content"></div></div></div>').insertBefore( "#footer" );
	var y =  ((gwd('h') / 2) - (hpx / 2));
    var x = (gwd('w') / 2) - (wpx / 2);
    y = (y > 90)?y:90;
    y = (y + hpx < (h-60) )?y:90;
    if(y == 90 &&  $(window).scrollTop() > 30 ) y = 20;
    y = y + $(window).scrollTop();
    dlog(y);
	$("div.encase").css({
		top: y,
		left: x,
		width:wpx,
		height:hpx
	});
		
	$("div.encase_content","#encase").load(url, function() {
		$("#encase").show();g_fadebox_added=1;
		//$("div.encase_content").jScrollPane();
		//$("div.encase .jScrollPaneContainer").css('width','780px');
	});
}

function encasehtml(html) {
    //$(document).scrollTop(0);//scroll to the top of the document
	show_fadebox();
	
	$("#liveArea").after('<div id="encase" class="encase"><div class="encase_content_wrapper"><div class="encase_content"></div></div></div>');
    var y =  ((gwd('h') / 2) - 298) + $(window).scrollTop();
    var x = (gwd('w') / 2) - 390;
    y = (y > 160)?y:160;
	$("div.encase").css({
		top: y,
		left: x
	});
		
	$("div.encase_content","#encase").html(html);
	
		$("#encase").show();g_fadebox_added=1;
		//$("div.encase_content").jScrollPane();
		//$("div.encase .jScrollPaneContainer").css('width','780px');
}

function encaseerror(options) {
    destroy_encaseerror();
    if(!options || !options.error){dlog('encase error');return false;}

	$("#liveArea").append('<div id="encaser" class="encaseerror" style="z-index:351;"><div class="encaseerror_content "></div></div>');
    if(options.height)
        $('#encaser.encaseerror').css('height',options.height);
	if(options.offsetDOM)
	{
	    $("div.encaseerror").css({ top: options.offsetDOM.offset().top + ( options.offsetAdd_Top || 0 ), left: options.offsetDOM.offset().left + ( options.offsetAdd_Left || 0 )});
	}
	else
	{
	    $("div.encaseerror").css({ top: ((650 / 2) ) + ($("div.encaseerror").height() / 2), left: (gwd('w') / 2) - ($("div.encaseerror").width() / 2)});
	}

    //if no title sent, set it to ERROR
	var etitle = (options.title)? options.title : 'ERROR' ;
	
	//build content for tooltip
	if( options.tooltip ){
	    var encDOM = $("div.encaseerror_content").html('<p class="small lh-normal HelvBold red">'+options.error +'</p>').css({ width:'170px',height:'30px' });
	    if( options.nored ) $("div.encaseerror_content p").removeClass('red');
	    encDOM.parent().css({ width:'213px' , height:'87px' , 'background': 'transparent url("/images/css/tooltip.png") no-repeat' , 'padding':'6px 12px 0 20px' , 'box-shadow':'none' , '-webkit-box-shadow':'none' , 'border':'none'});
	    }
	else if( options.large_tooltip ){
	    var encDOM = $("div.encaseerror_content").html('<p class="small lh-normal HelvBold red">'+options.error +'</p>').css({ width:'200px',height:'60px' });
	    if( options.nored ) $("div.encaseerror_content p").removeClass('red');
	    encDOM.parent().css({ width:'213px' , height:'120px' , 'background': 'transparent url("/images/css/tooltiplg.png") no-repeat' , 'padding':'6px 12px 0 20px' , 'box-shadow':'none' , '-webkit-box-shadow':'none' , 'border':'none'});
	    }
	else
	    var encDOM = $("div.encaseerror_content").html('<div class="closebtn">close</div><h2>'+etitle+'</h2><p>'+ options.error +'</p>');
	    
	if(options.modal !== true)
	    encDOM.parent().click(destroy_encaseerror).css('cursor','auto').find('.closebtn').css('cursor','pointer');
	else 
	    encDOM.children('.closebtn').click(destroy_encaseerror);
	    
}


// --------------------------------------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------------------------------------
//Event Handleres
$("div.encase_content a#close_overlay").die().live("click",hide_encase);
$("div.fadebox").die().live("click",hide_encase);

function move_encase() {
    var y =  ((gwd('h') / 2) - 298) + $(window).scrollTop();
    var x = (gwd('w') / 2) - 390;
    y = (y > 160)?y:160;
   
	$("div.encase,div.encaser").css({

		top:y,
		left:x 
	});
}





// --------------------------------------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------------------------------------
//Display Functions
function show_fadebox() {
	//log("adding fadebox");
		var thisbrowser,
			thisos;
	if($.client){
	    thisbrowser = $.client.browser;
	    thisos 		= $.client.os;
	}
	else{
	    thisbrowser = $.browser;
	    thisos 		= 'Windows';	    
	}
	var wheight = $(window).height();
	var bheight = $('body').height();
	var hheight = $('html').height();
	/*
	dlog( 'win height'+ $(window).height() );
	dlog( 'bodyheight' +bheight  );
	dlog( 'htmlheight' + $('html').height()  );*/
	
	if (thisos == 'Windows' && thisbrowser == 'Explorer') {
		$("html").css({'overflow-y':'scroll'});
		//if(bheight  > hheight  ) $("html").css({'height':'1200px'});
	} else {
		$("body").css({'overflow-y':'scroll'});
		if(hheight > 1190 ) $("body").css({'height':'1200px'});
	}
	$("#liveArea").after('<div class="fadebox"></div>');
	
	
	
	if (thisos == 'Windows' && thisbrowser == 'Explorer') {
		$("div.fadebox").css({
			width: '100%',
			height:bheight 
		});
	} else {
		$("div.fadebox").css({
			width: '100%',
			height: hheight
		});	
	}
	
	
	$("div.fadebox").show();
	
	
	
	//log("fadebox added");
	g_fadebox_added = 1;
}

function resize_fadebox() {
	if (g_fadebox_added == 1) {
		$("div.fadebox").remove();
		g_fadebox_added = 0;
		show_fadebox();
	}
}

function destroy_fadebox() {
	
	var thisbrowser = $.client.browser;
	var thisos 		= $.client.os;
	if (thisos == 'Windows' && thisbrowser == 'Explorer') {
		$("html").css('overflow','auto');
	} else {
		$("body").css('overflow','visible');
	}
    
	$("div.fadebox").remove();
}


function destroy_encase() {
	$("#encase").remove();	
	//$("#encaseerror").remove();	
}

function destroy_encaseerror(){

    $("#encaser.encaseerror").hide().remove();
}

function hide_encase() {
    destroy_encaseerror(true);
	destroy_encase();
	destroy_fadebox();
	g_fadebox_added = 0;
	if( typeof( inCheckout ) != "undefined" && inCheckout == true ) 
	{document.location = "/checkout.aspx";}
}	

// --------------------------------------------------------------------------------------------------------------------------------------------------------------
//Helper Functions


function gwd(dim) {
    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth,
        viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE

    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }	

    if (dim == 'w') { return(viewportwidth); }
    else if (dim == 'h') { return(viewportheight); }
}

