/* 	Custom Code for Princess.com Lightbox*/
/* Version 1.2 */

function lightbox_show_hide(){
	center_lightbox();
	lightBoxoverlay.show('fast', function(){
		lightBox.fadeIn('medium');
	});
 	//To Adjust the position of lighbox (Center of the page)
	lightBox.css("left", (jQuery(window).width() - lightBox.outerWidth())/2 + 'px');
	//Fix the issue of lightbox overlay not covering the whole height in IE6 
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		lightBoxoverlay.css("height", jQuery(document).height() ) ;
		} 
	// IE6 select menu overlapping over lightbox issue		
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		if(lightBox.css('display')=="block"){
			jQuery('#container select').css('visibility', 'hidden');
		}
		else { 
			jQuery('#container select').css('visibility', 'visible');
		  }
	  }
	//To hide the lightbox on clicking close window button
	jQuery('.lightbox-close-window a, .lightbox-close-window-top a').live('click',function() {
		jQuery('#lightbox-container').remove();
		jQuery('#container object').show();
		// IE6 select overlapping over popup div issue
		if(jQuery.browser.msie && jQuery.browser.version < 7){
			jQuery('#container select').css('visibility', 'visible');
	  	}
		return false;
	});
}
	jQuery('.lightbox-close-window').live('click',function() {
		return false;
	});
//To Adjust the position of lighbox when window is resized
function center_lightbox(){
	var topPosition=jQuery(window).scrollTop();
	lightBox.css('top',topPosition+50);
	jQuery(window).resize(function() {
		lightBox.css("left", (jQuery(window).width() - lightBox.outerWidth())/2  + 'px');
	});
}

function openLightbox(url){
	buildLightboxContainer();
	if(targetElement!== undefined){
		url= url+ " "+targetElement;
	}
	var extension = url.substr( (url.lastIndexOf('.') +1) );
	   switch(extension) {
        case 'jpg':
        case 'png':
        case 'gif':		
		var img= $("<img>").attr('src', url).load(url, function () {
			lightBox.css('padding-top','20px');
			lightBox.prepend(img);
			lightbox_show_hide();
		});
		buildLightBoxCloseElements();
		lightbox_show_hide();
		break;
	default:
	lightBox.load(url, function () {
		buildLightBoxCloseElements();
		lightbox_show_hide();
	});
   }
}

function openIframe(){
	var $iframe = jQuery("<iframe id='iframe-popup-page' src='"+url+"' width='"+iframeWidth+"'  height='"+iframeHeight+"' frameBorder='0' onLoad='openIframeLinksInParentWindow()' /> ");
	buildLightboxContainer();
	buildLightBoxCloseElements();
	lightBox.addClass('iframe-lightbox');
	$iframe.prependTo("#lightbox-container .lightbox");
	 lightbox_show_hide();
}

function openIframeBox(urls, iWidth, iHeight){
	//erase the error msg on popup load
	jQuery('#showError').html('');
    var $iframe = jQuery("<iframe id='iframe-popup-page' src='"+urls+"' width='"+iWidth+"'  height='"+iHeight+"' frameBorder='0' onLoad='openIframeLinksInParentWindow()' /> ");
    buildLightboxContainer();
    buildLightBoxCloseElements();
    lightBox.addClass('iframe-lightbox');
    $iframe.prependTo("#lightbox-container .lightbox");
    lightbox_show_hide();
    jQuery('iframe').iframeAutoHeight({heightOffset: 0});// iframe dynamic height
    return false;
}

function loadInsideLightbox(){
	lightBox.load(url, function () {
		buildLightBoxCloseElements();
		lightbox_show_hide();
	});
}


jQuery('.open-iframe, .iframe, .pcfb').live('click',function(){ // Open iframe inside Lightbox
	iframeWidth = 734;
	iframeHeight = 600;
	url= jQuery(this).attr('href');
	if (jQuery(this).attr('data-width') !== undefined){
		iframeWidth= jQuery(this).attr('data-width');
	}
	else{
	iframeWidth = 734;
		}
	if (jQuery(this).attr('data-height') !== undefined){
		iframeHeight= jQuery(this).attr('data-height');
	}
	else{
	iframeHeight = 600;
		}
	openIframe();
	jQuery('iframe').iframeAutoHeight({heightOffset: 0});// iframe dynamic height

	return false;
}); 

function openIframeLinksInParentWindow(){
	 setTimeout( function() {
		//Force links inside iframe page to open in parent page excluding specific links (with class open-iframe, .open-lightbox and target attribute _blank).
		jQuery('#iframe-popup-page').contents().find('a').attr('target','_blank').addClass('new-window');
		jQuery('#iframe-popup-page').contents().find('a:not(.open-lightbox, .open-iframe, .new-window)').attr('target','_parent');
          }, 1000 );
	}

jQuery('.open-lightbox').live('click',function(){
	url= jQuery(this).attr('href');
	//Code to load only the content from the specified container inside the page.
	targetElement = jQuery(this).attr('data-target');
	if(jQuery('#lightbox-container').length >=1){
		loadInsideLightbox(url);
		}
	else{
		openLightbox(url);
	}
	return false;
});

function buildLightboxContainer(){
	jQuery('#lightbox-container').remove();
	jQuery('body').append('<div id="lightbox-container">');
	jQuery('#lightbox-container').prepend('<div class="lite-popup"><div id="lightbox-overlay"></div><div class="lightbox">');
	lightBox=jQuery('#lightbox-container .lightbox');
	lightBoxoverlay= jQuery('#lightbox-overlay');
	}
function buildLightBoxCloseElements(){
	jQuery('#lightbox-container .lightbox').append('<div class="lightbox-close-window"> <a href="javascript:void(0)">Close Window</a></div>\
	<div class="lightbox-close-window-top"> <a href="javascript:void(0)"><img src="/images/global/lightbox_close_btn.png" height="32" width="32"></a></div>');	
	}

/*
Plugin: iframe autoheight jQuery Plugin 1.4.1
see README: http://github.com/house9/jquery-iframe-auto-height
*/
(function(a){a.fn.iframeAutoHeight=function(b){function d(a){c.debug&&c.debug===!0&&window.console&&console.log(a)}var c=a.extend({heightOffset:0,callback:function(a){},debug:!1},b);d(c),a(this).each(function(){function f(a){var b;e(a)?b=a.contentWindow.document.body.scrollHeight+c.heightOffset:b=a.contentWindow.document.body.offsetHeight+c.heightOffset,d("New Height: "+b),a.style.height=b+"px",c.callback({newFrameHeight:b})}function e(a){if(a.contentWindow.document.compatMode&&b()&&"function"==typeof window.ActiveXObject){d("IE Quirks mode");return!0}return!1}function b(){return typeof window.ActiveXObject!="undefined"}d(this);if(a.browser.safari||a.browser.opera){d("browser is webkit or opera"),a(this).load(function(){var a=this;a.style.height="0px";var b=function(){f(a)};setTimeout(b,0)});var g=a(this).attr("src");a(this).attr("src",""),a(this).attr("src",g)}else a(this).load(function(){f(this)})})}})(jQuery);



	

