/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
 */

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS;
var browser;
var version;
var total;
var thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo() {
    //alert("getBrowserInfo");
    if (checkIt('konqueror')) {
        browser = "Konqueror";
        OS = "Linux";
    }
    else if (checkIt('safari')) browser 	= "Safari"
    else if (checkIt('omniweb')) browser 	= "OmniWeb"
    else if (checkIt('opera')) browser 		= "Opera"
    else if (checkIt('webtv')) browser 		= "WebTV";
    else if (checkIt('icab')) browser 		= "iCab"
    else if (checkIt('msie')) browser 		= "Internet Explorer"
    else if (!checkIt('compatible')) {
        browser = "Netscape Navigator"
        version = detect.charAt(8);
    }
    else browser = "An unknown browser";

    if (!version) version = detect.charAt(place + thestring.length);

    if (!OS) {
        if (checkIt('linux')) OS 	= "Linux";
        else if (checkIt('x11')) OS 	= "Unix";
        else if (checkIt('mac')) OS 	= "Mac"
        else if (checkIt('win')) OS 	= "Windows"
        else OS 			= "an unknown operating system";
    }
}

function checkIt(string) {
    //alert("checkIt");
    place = detect.indexOf(string) + 1;
    thestring = string;
    return place;
}

/*-----------------------------------------------------------------------------------------------*/

//Event.observe(window, 'load', initialize, false);
//Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {
    yPos : 0,
    xPos : 0,

    initialize: function() {
    //alert("initializare");
    getBrowserInfo();
    },
	
    // Turn everything on - mainly the IE fixes
    activate: function(){
        //alert("activate:");
        if (browser == 'Internet Explorer'){
            //alert("cacat de internet explorer:");
            this.getScroll();
            this.prepareIE('100%', 'hidden');
            this.setScroll(0,0);
            this.hideSelects('hidden');
        }
    //this.displayLightbox("block");
    },
	
    // Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
    prepareIE: function(height, overflow){
        //alert("prepareIE:");
        bod = document.getElementsByTagName('body')[0];
        bod.style.height = height;
        bod.style.overflow = overflow;
  
        htm = document.getElementsByTagName('html')[0];
        htm.style.height = height;
        htm.style.overflow = overflow; 
    },
	
    // In IE, select elements hover on top of the lightbox
    hideSelects: function(visibility){
        //alert("hideSelects:");
        selects = document.getElementsByTagName('select');
        for(i = 0; i < selects.length; i++) {
            selects[i].style.visibility = visibility;
        }
    },
	
    // Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
    getScroll: function(){
        //alert("getScroll:");
        if (self.pageYOffset) {
            this.yPos = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){
            this.yPos = document.documentElement.scrollTop; 
        } else if (document.body) {
            this.yPos = document.body.scrollTop;
        }
    },
	
    setScroll: function(x, y){
        //alert("setScroll:");
        window.scrollTo(x, y); 
    },
	
    actions: function(){
        //alert("actions:");
        lbActions = document.getElementsByClassName('cancelAction');
        for(i = 0; i < lbActions.length; i++) {
            Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
            lbActions[i].onclick = function(){
                return false;
            };
        }

    },
	
    // Example of creating your own functionality once lightbox is initiated
    deactivate: function(){
        //alert("deactivate");
        Element.remove($('overlay'));
        try{
            Element.remove($('lightbox'));
        }catch(ex){
            Element.remove($('biglightbox'));
        }
        
		
        if (browser == "Internet Explorer"){
            this.setScroll(0,this.yPos);
            this.prepareIE("auto", "auto");
            this.hideSelects("visible");
        }
		
    //this.displayLightbox("none");
    }
}

function deactivate(){
        //alert("deactivate");
        Element.remove($('overlay'));
        try{
            Element.remove($('lightbox'));
        }catch(ex){
            Element.remove($('biglightbox'));
        }


        if (browser == "Internet Explorer"){
            this.setScroll(0,this.yPos);
            this.prepareIE("auto", "auto");
            this.hideSelects("visible");
        }

    //this.displayLightbox("none");
    }

/*-----------------------------------------------------------------------------------------------*/

/**
 * <b><u>innerHtml</u></b> - text reprezentand un bloc HTML care va aparea in interiorul unui lightBox<br/>
 */
function addLightboxMarkup(innerHtml) {
    //alert("addLightboxMarkup");
    messageBox = document.getElementById("lbLoadMessage");
    var lBox = new lightbox();
    
    if (messageBox != null){
        messageBox.innerHTML = innerHtml;
        lBox.actions();
    }else{
        lBox.activate();
        bod 		= document.getElementsByTagName('body')[0];
        overlay 		= document.createElement('div');
        overlay.id		= 'overlay';
        overlay.style.width = screen.width;
        //overlay.style.height = screen.height;
        var arrayPageSize = getPageSize();
        overlay.style.height =  (arrayPageSize[1] +'px');

        lb			 = document.createElement('div');
        lb.id		 = 'lightbox';
        lb.innerHTML = '<div class="closeContainer" align="right"><a href="#" rel="deactivate" class="cancelAction" >close </a></div>'+
                       '<div class="innerLightbox">'+
                       '<div id="lbLoadMessage">'+innerHtml+'</div>'+
                       '</div>';

        bod.appendChild(overlay);
        bod.appendChild(lb);
        lBox.actions();
    }
    return false;
}

function 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;
}


function addLightboxMarkupWH (innerHtml, width, top) {
    //alert("addLightboxMarkup");
    messageBox = document.getElementById("lbLoadMessage");
    var lBox = new lightbox();

    if (messageBox != null){
        messageBox.innerHTML = innerHtml;
        lBox.actions();
    }else{
        lBox.activate();
        bod 		= document.getElementsByTagName('body')[0];
        overlay 		= document.createElement('div');
        overlay.id		= 'overlay';
        overlay.style.width = screen.width;
        var arrayPageSize = getPageSize();
        overlay.style.height =  (arrayPageSize[1] +'px');

        var centering = (screen.width - width)/2;

        lb			= document.createElement('div');
        lb.id		= 'lightbox';
        lb.style.width    = "auto";//width+"px";
        lb.style.height   = "auto";//height+"px";

        lb.style.left = centering+"px"; //centrare orizontala
        lb.style.top = top+"px";
        lb.style.margin = "0px";
        lb.style.padding = "0px";

        lb.innerHTML	= '<div id="lbLoadMessage">'+innerHtml+'</div>';
        bod.appendChild(overlay);
        bod.appendChild(lb);
        lBox.actions();

    }
    return false;
}
/**
 * <b><u>innerHtml</u></b> - text reprezentand un bloc HTML care va aparea in interiorul unui lightBox<br/>
 */
function addPictureBigLightboxMarkup(innerHtml) {
    //alert("addLightboxMarkup");
    messageBox = document.getElementById("lbLoadMessage");
    
    if (messageBox != null){
        messageBox.innerHTML = innerHtml;
    }else{
        var lBox = new lightbox();
        lBox.activate();
    
        bod 		= document.getElementsByTagName('body')[0];
        overlay 		= document.createElement('div');
        overlay.id		= 'overlay';
        overlay.style.width = screen.width;
        //overlay.style.height = screen.height;
                var arrayPageSize = getPageSize();
        overlay.style.height =  (arrayPageSize[1] +'px');

    
        lb			= document.createElement('div');
        lb.id		= 'biglightbox';
    
        lb.innerHTML	= '<div id="lbLoadMessage">'+innerHtml+'</div>';
        bod.appendChild(overlay);
        bod.appendChild(lb);
        lBox.actions();
    }
    return false;
}

/**
 * functia deschide in pagina un popup de dimansiune mai mare decat cel de alertare ce va contine
 * texul rezultat in urma unei cereri http.<br/>
 * <b><u>url</u></b> - url-ul catre carese va face cererea<br/>
 * <b><u>postParams</u></b> - eventuala lista cu parametri de trimis sub forma unei cereri POST,
 * reprezentand un queryString form-urlencoded (vezi functia <b>getURLEncodedQueryFromContainer</b>).
 * Daca nu este cazul poate avea valoarea null sau sirul vid si va fi ignorat<br/>
 * <b><u>errInnerHTML</u></b> - text reprezentand un bloc HTML care sa apara cand status-ul
 * este diferit de 200 OK<br/>
 * <b><u>sessionExpiredInnerHTML</u></b> - text reprezentand un bloc HTML care sa apara cand
 * sesiunea a expirat<br/>
 */
function addBigLightboxMarkup(url, postParams, errInnerHTML, sessionExpiredInnerHTML) {
    messageBox = document.getElementById("lbLoadMessage");
    var lBox = new lightbox();
    
    if (messageBox != null){
        messageBox.innerHTML = "loading";
        ajaxRequest('lbLoadMessage', url, lBox);
    }else{
        lBox.activate();
    
        bod 		= document.getElementsByTagName('body')[0];
        overlay 		= document.createElement('div');
        overlay.id		= 'overlay';
        overlay.style.width = screen.width;
        //overlay.style.height = screen.height;
        var arrayPageSize = getPageSize();
        overlay.style.height =  (arrayPageSize[1] +'px');

    
        lb			= document.createElement('div');
        lb.id		= 'biglightbox';
        lb.style.top = '220px'; //test
    
        lb.innerHTML	= '<div id="lbLoadMessage">loading</div>';
        bod.appendChild(overlay);
        bod.appendChild(lb);

        if (postParams != null && postParams.length > 0){
            ajaxPostRequest('lbLoadMessage', url, postParams, lBox, errInnerHTML, sessionExpiredInnerHTML);
        }else{
            ajaxRequest('lbLoadMessage', url, lBox, errInnerHTML, sessionExpiredInnerHTML);
        }
    }
    
    return false;
}

function testLightBox(){
    var innerHtml = '<div class="popup1" id="popup">'+
    '<div class="popup_header1">Sterge de la favorite</div>'+
    '<div class="popup_body1">Esti sigur ca vrei sa-l stergi pe utilizatorul xxx de la favorite?</div>'+
    '<div class="popup_footer1">'+
    '<table width="100%">'+
    '<tr>'+
    '<td width="50%" align="center"><a href="http://www.evz.ro">[ok]</a></td>'+
    '<td width="50%" align="center"><span class="lb">&nbsp;</span><a href="http://www.evz.ro" rel="deactivate" class="cancelAction" >Anuleaza</a><span class="rb">&nbsp;</span></td>'+
    '</tr>'+
    '</table>'+
    '</div>'+
    '</div>';
    addLightboxMarkup(innerHtml);
}

function addUserPreviewLightBox (imgUrl, nume, varsta, sex, zodie, localitate, ocupatie, email) {
    if (varsta == 0){
        varsta = '&nbsp;&nbsp;&nbsp;-';
    }
    if (localitate == ''){
        localitate = '&nbsp;&nbsp;&nbsp;-';
    }
    if (ocupatie == ''){
        ocupatie = '&nbsp;&nbsp;&nbsp;-';
    }
    var innerHtml = '<div class="preview">'+
                    '<p><span class="nr_mesaje">'+ nume +'</span></p>'+
                    '<table cellspacing="0" cellpadding="0" border="0">'+
                    '<tr><td><img class="general_mm" src="'+imgUrl+'" alt="" /></td>'+
                        '<td>'+
                            '<table cellspacing="5" cellpading="0" border="0" class="info">'+
                                '<tr><td>Nume: '+nume+'</td></tr>'+
                                '<tr><td>Varsta: '+varsta+'</td></tr>'+
                                '<tr><td>Sex: '+sex+'</td></tr>'+
                                '<tr><td>Zodie: '+zodie+'</td></tr>'+
                                '<tr><td>Localitate: '+localitate+'</td></tr>'+
                                '<tr><td>Ocupatie: '+ocupatie+'</td></tr>'+
                                //'<tr><td>E-mail: '+email+'</td></tr>'+
                           '</table>'+
                        '</td>'+
                    '</tr>'+
                    '<tr><td colspan="2" align="center" style="margin-top:10px;">'+
                            '<span class="lb">&nbsp;</span><a href=\"#\" class=\"cancelAction\" rel=\"deactivate\">inchide</a><span class="rb">&nbsp;</span>'+
                    '</td></tr>'+
                    '</table>'+
                    '</div>';
    addLightboxMarkupWH(innerHtml,400,200);
}

function addUserPreviewLightBox2 (imgUrl, nume, varsta, sex, zodie, localitate, ocupatie) {
    if (varsta == 0){
        varsta = '&nbsp;&nbsp;&nbsp;-';
    }
    if (localitate == ''){
        localitate = '&nbsp;&nbsp;&nbsp;-';
    }
    if (ocupatie == ''){
        ocupatie = '&nbsp;&nbsp;&nbsp;-';
    }
    var innerHtml = '<div class="closeContainer" align="right"><a href="#" rel="deactivate" class="cancelAction" >close </a></div>'+
                    '<div class="innerLightbox">'+
                    '<div class="preview">'+
                    '<span class="nr_mesaje">'+ nume +'</span>'+
                    '<div class="popup_body1">'+
                    '<table cellspacing="3" cellpadding="0" border="0">'+
                    '<tr><td><img class="general_mm" src="'+imgUrl+'" alt="" /></td>'+
                        '<td>'+
                            '<table cellspacing="3" cellpading="0" border="0" class="info" style="color:#000000;font-size:12px;">'+
                                '<tr><td>Nume: '+nume+'</td></tr>'+
                                '<tr><td>Varsta: '+varsta+'</td></tr>'+
                                '<tr><td>Sex: '+sex+'</td></tr>'+
                                '<tr><td>Zodie: '+zodie+'</td></tr>'+
                                '<tr><td>Localitate: '+localitate+'</td></tr>'+
                                '<tr><td>Ocupatie: '+ocupatie+'</td></tr>'+
                           '</table>'+
                        '</td>'+
                    '</tr>'+
                    '</table>'+
                    '</div>'+
                    '<div class="popup_footer1" align="center">'+
                        '<div align="center" style="height:11px;font-size:11px;" border="1">'+
                            '<span class="lb">&nbsp;</span><span class="over_rite"> <a href="#" rel="deactivate" class="cancelAction">inchide</a></span> <span class="rb">&nbsp;</span>'+
                        '</div>'+
                    '</div>'+
                    '</div>'+
                    '</div>';
    addLightboxMarkupWH(innerHtml,400,200);
}


function window_test (link) {
    alert('!');
    var win = new Window({className: "spread", title: "Adauga poza", top:70, left:100, width:300, height:200, url:link , showEffectOptions: {duration:1.5}}); win.show();
}
