/*
	Light Dialog
	Skrypt tworzacy okienka dialogowe z zawartoscia Ajaxowa
	stworzony na podstawie kodu z Lightbox 
	(http://huddletogether.com/projects/lightbox/)
*/


//	Konfiguracja {{{
// If you would like to use a custom loading image or close button reference them in the next two lines.
var ld_loadingImage = '/images/ld_loading.gif';
var ld_closeButton  = '/images/ld_close.gif';
var ld_useOverlay   = false;
// }}}

// Funkcje pomocnicze {{{ 

/*
	funkcje z Lightbox
	(http://huddletogether.com/projects/lightbox/)
*/

//
// ld_getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function ld_getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// ld_getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function ld_getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// ld_pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function ld_pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}



//
// ld_addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function ld_addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

//}}}

// initLightDialog() {{{
/*
 	Funkcja initLightDialog() jest uruchamiana podczas ładowania strony, fukcja poszukuje linków rel lightdialog (rel="lightdialog").
 	W znaleźionych linkach lightdialog funkcja dodaje obsluge zdarzenia onClick które aktywuje kupowanie przez ajax
 	Ta fukcja również dodaje do body div'a który bedzie kontenerem na dene z ajaxa
 */
function initLightDialog()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// przeszukanie wszystkich linków
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightdialog")){
			anchor.onclick = function () {showLightDialog(this); return false;}
		}
	}

	// dalsza cześc kodu wstawie kod html, ld_overlay wstawiany, jesli ld_useOverlay=true
	// 
	//  <div id="ld_overlay"></div>
    //  <div id="ld_loading">
	//     <a href="#" onclick="hideLightDialog(); return false;"><img id="ld_loadingImage" /></a>
    //  </div>
	//  <div id="lightdialog">
	//	    <a href="#" onclick="hideLightDialog(); return false;" title="zamknij">
	//		    <img id="ld_closeButton" />		
	//	    </a>
	//	    <div id="ld_content">
	//      </div>
	//  </div>
	
	var objBody = document.getElementsByTagName("body").item(0);

	// ld_overlay {{{
    if( ld_useOverlay ) {
        var objOverlay = document.createElement("div");
        objOverlay.setAttribute('id','ld_overlay');
        objOverlay.onclick = function () {hideLightDialog(); return false;}
        objOverlay.style.display = 'none';
        objOverlay.style.position = 'absolute';
        objOverlay.style.top = '0';
        objOverlay.style.left = '0';
        objOverlay.style.zIndex = '140';
        objOverlay.style.width = '100%';
        objBody.insertBefore(objOverlay, objBody.firstChild);
    }
    //}}}

	// ld_loading, ld_loadingImage {{{
	var objLoading = document.createElement("div");
	objLoading.setAttribute('id','ld_loading');
	objLoading.onclick = function () {hideLightDialog(); return false;}
	objLoading.style.display = 'none';
	objLoading.style.position = 'absolute';
	objLoading.style.zIndex = '150';
    if( ld_useOverlay ) {
        objBody.insertBefore(objLoading, objOverlay.nextSibling);
    } else {
        objBody.insertBefore(objLoading, objBody.firstChild);
    }


    var objText1 = document.createTextNode('ładowanie danych');
    var objText2 = document.createTextNode('proszę czekać');
    var theBR    = document.createElement('br');
    var objLoadingText = document.createElement("p");
    objLoadingText.appendChild(objText1);
    objLoadingText.appendChild(theBR);
    objLoadingText.appendChild(objText2);
    objLoading.appendChild(objLoadingText);

	var ld_imgPreloader = new Image();

	// if loader image found, create link to hide lightdialog and create loadingimage
	ld_imgPreloader.onload=function(){

		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightDialog(); return false;}
		objLoading.appendChild(objLoadingImageLink);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = ld_loadingImage;
		objLoadingImage.setAttribute('id','ld_loadingImage');
		objLoadingImageLink.appendChild(objLoadingImage);

		ld_imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}
    ld_imgPreloader.src = ld_loadingImage;


    //}}}
	
    //{{{ lightdialog

	var objLightdialog = document.createElement("div");
	objLightdialog.setAttribute('id','lightdialog');
	objLightdialog.style.display = 'none';
	objLightdialog.style.position = 'absolute';
	objLightdialog.style.zIndex = '100';	
	objLightdialog.style.width = '400px';	
	objBody.insertBefore(objLightdialog, objLoading.nextSibling);

    // przycisk zamknij {{{
	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','zamknij');
	objLink.onclick = function () {hideLightDialog(); return false;}
	objLightdialog.appendChild(objLink);

	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	imgPreloadCloseButton.onload=function(){

		var objCloseButton = document.createElement("img");
		objCloseButton.src = ld_closeButton;
		objCloseButton.setAttribute('id','ld_closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objLink.appendChild(objCloseButton);

		return false;
	}

	imgPreloadCloseButton.src = ld_closeButton;
    //}}}

    // div #ld_content {{{
	var objLightdialogContent = document.createElement("div");
	objLightdialogContent.setAttribute('id','ld_content');
	objLightdialog.appendChild(objLightdialogContent);

    //}}}

    //}}}

}

//}}}
// showLightDialog() {{{

//{{{ helpers

var arrayPageSize;
var arrayPageScroll;

function ld_setPageVars(){
	arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();
}



function ld_showLoading(){
	var objLoading = document.getElementById('ld_loading');

	// center loading if it exists
	if (objLoading) {
		objLoading.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoading.clientHeight) / 2) + 'px');
		objLoading.style.left = (((arrayPageSize[0] - 20 - objLoading.clientWidth) / 2) + 'px');
		objLoading.style.display = 'block';
	}
}

function ld_hideLoading(){
	var objLoading = document.getElementById('ld_loading');
    if (objLoading) {	objLoading.style.display = 'none'; }
}


function ld_showOverlay(){
    if( !ld_useOverlay ) return;
	var objOverlay      = document.getElementById('ld_overlay');

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
    
}

function ld_hideOverlay(){
    if( !ld_useOverlay ) return;
	var objOverlay      = document.getElementById('ld_overlay');
	objOverlay.style.display = 'none';
}


function ld_showDialog(){

    ld_hideLoading();
    ld_hideOverlay();

	var objLightdialog  = document.getElementById('lightdialog');
	var objContent      = document.getElementById('ld_content');
    if( ld_useOverlay ) {
        var objOverlay  = document.getElementById('ld_overlay');
    }

	// center lightdialog and make sure that the top and left values are not negative
	// and the image placed outside the viewport 
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 200) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - 200) / 2);
	
	objLightdialog.style.top  = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightdialog.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

	objLightdialog.style.display = 'block';
	
    if( ld_useOverlay ) {
        arrayPageSize = getPageSize();
        objOverlay.style.height = (arrayPageSize[1] + 'px');
    }
}

function ld_hideDialog(){
	var objLightdialog  = document.getElementById('lightdialog');
	objLightdialog.style.display = 'none';
}

function ld_resetDialog(){
	var objContent      = document.getElementById('ld_content');
    objContent.innerHTML='';
    ld_hideDialog();
}

//}}}

/**
 * Funkcja wyswietla "okienko" ajaxowe z mozliwośća kupan towaru
 */
function showLightDialog(objLink){

    ld_setPageVars();

    new Ajax.Updater(
        'ld_content', 
        objLink.href,
        {
            asynchronous:true, 
            evalScripts:true, 
            onComplete:function(request, json){ ld_hideLoading() }, 
            onFailure:function(request,  json){ ld_hideOverlay();alert('Wystąpił bład');ld_resetDialog();}, 
            onLoading:function(request,  json){ ld_hideDialog();ld_showOverlay();ld_showLoading();}, 
            onSuccess:function(request,  json){ ld_showDialog();}
        });
    return false;
}
//}}}
// hideLightDialog() {{{
function hideLightDialog() {

    ld_hideOverlay();
    ld_hideLoading();
    ld_hideDialog();
	return false;
}
//}}}

ld_addLoadEvent(initLightDialog);


