
$(function() {

    var el = document.getElementById('popup');
//    if(el && el.src && !readCookie('nobugPopup')) {
    if(el && el.innerHTML && !readCookie('nobugPopup')) {
 //       var html  = el.src;
          var html  = el.innerHTML;

        var title = el.alt?el.alt:'ANNOUNCEMENT';

        imgW = el.width?el.width:$(el).width();
        imgH = el.height?el.height:$(el).height();
        var wid   = Math.min(imgW,788);
        var hgt   = imgH;
	var hgt   = 60; //razvan
	var wid	  = 400; //razvan
        var left  = Math.max((1003-wid)/2,210);

        $(el).remove();
//        $('#main').append('<div id="winPopup" style="width:'+wid+'px;left:'+left+'px;"><div class="title"><img src="'+BASE+'/img/cancel.png" alt="x" width="16" height="16" onclick="$(\'#winPopup\').remove();return false;" class="close" title="Close popup" />'+title+'</div><div class="body"><img src="'+html+'" alt="" /></div></div>');
        $('#main').append('<div id="winPopup" style="width:'+wid+'px;height:'+hgt+'px;left:'+left+'px;"><div class="title"><img src="'+BASE+'/img/cancel.png" alt="x" width="16" height="16" onclick="$(\'#winPopup\').remove();return false;" class="close" title="Close popup" />'+title+'</div><div class="body">'+html+'</div></div>');
 //       createCookie('nobugPopup',true);
        window.setTimeout('removePopup()',10000);
    }

    $('span.email').each(function() {
        var content = $(this).html();
        var filter  = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/;
        //replace all spaces
        content = content.replace(/ /g,'');
        //replace [at] with @, [dot] with .
        content = content.replace('[at]','@');
        content = content.replace('[dot]','.');
	if (filter.test(content)) {//if result is an email address, replace it
	   $(this).html('<a href="mailto:'+content+'">'+content+'</a>');
	}
    });


    $('a.external').attr('target','_blank');

    if($('#showmoreinfoform').length) {
        $('#showmoreinfoform').show();
        $('#hidemoreinfoform').hide();
        $('#moreinfoform').hide();
        $('#showmoreinfoform').click(function(){
            $('#moreinfoform').show();
            $('#showmoreinfoform').hide();
            $('#hidemoreinfoform').show();
            return false;
        });

        $('#hidemoreinfoform').click(function(){
            $('#moreinfoform').hide();
            $('#showmoreinfoform').show();
            $('#hidemoreinfoform').hide();
            return false;
        });
    }



    if($('#trialdownload').length) {
	$('#trialdownload').click(function(){

	var widthh = 740;
	var heightt = 740;

	var x = parseInt(window.screen.width) / 2 - (widthh / 2); 
	var y = parseInt(window.screen.height) / 2 - (heightt / 2);
	var url = 'http://nobug.ro/license';
	var newwindow = '';
	if (!newwindow.closed && newwindow.location) {
		newwindow.location.href = url;
	}
	else {
		newwindow=window.open(url,'name','addressbar=no,fullscreen=no,toolbar=0,scrollbars=no,width='+widthh+',height='+heightt+',location=no,statusbar=no,menubar=no,resizable=yes');

		if (!newwindow.opener) newwindow.opener = self;
	}
	
	if (window.focus) {newwindow.focus()}
	return false;
	});
    }


    var extImages = [];


    $('#fromDB a').each(function() {
        var href        =   $(this).attr('href');
        var external    =   /:\/\//;
        var image       =   /(\.jpeg)|(\.gif)|(\.jpg)|(\.png)$/i;
        if (external.test(href)) {
            $(this).addClass('external').attr('target','_blank');
        } else if(image.test(href)) {

            //preload images 
            this.pos = extImages.length;
            extImages[this.pos] = new Image();
            extImages[this.pos].src=href;

            $(this).click(function() {
                var width=700;
                var height=700;
                var left=10;
                var top=10;
                var sW=800;
                var sH=800;


                if(screen.availWidth) {
                    sW = screen.availWidth;
                }

                if(screen.availHeight) {
                    sH = screen.availHeight;
                }
                if(extImages[this.pos].width) {
                    width = Math.min(sW,extImages[this.pos].width+30);
                }
                if(extImages[this.pos].height) {
                    height = Math.min(sH,extImages[this.pos].height+30);
                }
 
                left = (sW-width)/2;
                top = (sH-height)/2;

                var win = window.open(this.href,'viewimg','height='+height+',width='+width+',top='+top+',left='+left+',scrollbars=yes,location=no,directory=no,status=no,menubar=no,toolbar=no,resizable=yes,dependent=yes');
                
                if(win.focus) {
                    win.focus();
                }
                return false;
            }); 
        } else {
            //alert(href+' is leave intact');
        }
    });

});



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function removePopup() {
    el = document.getElementById('winPopup');
    if(el) {
        $(el).hide('slow');
        //$(el).remove();
    } 
}



