var sSubmittedForm = '';
function disableMultipleSubmit( sForm ){
    if ( '' == sSubmittedForm ){
        sSubmittedForm = sForm;return true;
    }
    return false;
}

function jump(){
    sSelectedMonth = document.calendar.calendar_month.options[ document.calendar.calendar_month.selectedIndex ].value;
    sSelectedYear = document.calendar.calendar_year.options[document.calendar.calendar_year.selectedIndex].value
    window.location = sRoot+ 'arhiva/stiri/?c=m&v=' + sSelectedYear + '-' + sSelectedMonth;
}



function bookmarksite(title, url) {
		if (document.all)
			window.external.AddFavorite(url, title);
		else if (window.sidebar)
			window.sidebar.addPanel(title, url, "");
	}

function createTooltip( sId, sTitle, sText, sStyle ) {

 			new Tip( sId , sText , {
				title: sTitle,
				style: sStyle,
				fixed: true,
				//hideOn: { element: 'tip', event: 'mouseout' },
				hideOn: false, // disable the normal hide event, because we only want to use the hideAfter option
				hideAfter: 0.2,
				hideOthers : true,
				hook: { target: 'bottomRight', tip: 'topLeft' }
			});


}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function parseTT( sTableId, sTabContainer ){

	if ( !/tt_table/i.test( sTableId ) ) return;
	oTableElement = document.getElementById( sTableId );
	if ( null == oTableElement ) return;
	if ( null == sTabContainer ) sTabContainer = "li";
	oTTable = new TTable( oTableElement, sTabContainer );
	return;

}

function TTable( oElement, sTabContainer ){

    this.sId = oElement.id.substr( 9 );
    aTds = oElement.getElementsByTagName(sTabContainer);
    this.aTabs= new Array();
	for (var i = 0; i < aTds.length; i++) {
	    if ( /tt_tab/i.test( aTds[i].id ) ){
	        this.aTabs.push( new TTab( aTds[i] ) );
	    }
	}

	var _o = this;
	for (var i = 0; i < this.aTabs.length; i++) {
        this.aTabs[i].oElement.onclick = function() {
            _o.show(this); return false;
		}
	}

}

TTable.prototype.show = function( oTrigger ) {

    oTab = null;
    iTabIndex = null;

    for (var i = 0; i < this.aTabs.length; i++) {
        if ( oTrigger.id == this.aTabs[i].oElement.id ){
            oTab = this.aTabs[i];
            iTabIndex = i;
            break;
        }
    }

    if ( oTab==null ) return false;

	for (var i = 0; i < this.aTabs.length; i++) {
		if ( 0 == i ){
			this.aTabs[i].oElement.className='first';
		}else if ( i == ( this.aTabs.length - 1 ) ){
			this.aTabs[i].oElement.className='last';
		}else{
			this.aTabs[i].oElement.className='';
		}
	}
    this.aTabs[iTabIndex].oElement.className = ( ''==this.aTabs[iTabIndex].oElement.className ? 'selected' : this.aTabs[iTabIndex].oElement.className + ' selected');

    this.hideAll();
    document.getElementById( 'tt_content_'+this.aTabs[iTabIndex].sId ).style.display="block";

}

TTable.prototype.hideAll = function() {
    for (var i = 0; i < this.aTabs.length; i++) {
        document.getElementById( 'tt_content_'+this.aTabs[i].sId ).style.display="none";
    }
}

function TTab( oElement ){
    this.sId = oElement.id.substr( 7 );
    this.oElement   = oElement;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function swPollView( iPollId, _embed ){

	_embed  = ( _embed ? true : false );
	_prefix = ( _embed ? "embed_polls" : "polls");

    oFCont = document.getElementById( _prefix + iPollId + "_form" );
    if ( oFCont == null ) return;
    oVCont = document.getElementById( _prefix + iPollId + "_view" );

    if ( 'block'==oFCont.style.display){
        oFCont.style.display = 'none';
        oVCont.style.display = 'block';
        if ( ''==oVCont.innerHTML ){ xajax_pollfetch( iPollId, _embed ) };
    }else{
        oFCont.style.display = 'block';
        oVCont.style.display = 'none';
    }

}

function pollvote( iPollId, _embed ){

	_embed  = ( _embed ? true : false );
	_prefix = ( _embed ? "embed_polls" : "polls");

    oForm = document.getElementsByName( _prefix + iPollId )[0];
    iOptionId = getpolloption( oForm );
    if ( iOptionId==null ) return;
    xajax_pollvote( iPollId, iOptionId, _embed );


}

function getpolloption( oForm ){
    for(var i=0; i<oForm.poll_option.length; i++){
        if ( oForm.poll_option[i].checked ) return oForm.poll_option[i].value;
    }
    return null;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function FPGallery( sId ){
	this.sId = sId;
	this.aItems = new Array();
	this.oCurrent = null;
}

FPGallery.prototype.add = function( sType, sId, sContent, sDescription, sUrl ){
	this.aItems.push( new FPGalleryItem( sType, sId, sContent, sDescription, sUrl ) );
}

FPGallery.prototype.load = function( sId ){

	if ( this.oCurrent!=null && this.oCurrent.sId==sId ) return;

	document.getElementById( this.sId + '_player' ).style.display = 'none';
	for (var i = 0; i < this.aItems.length; i++){
		if ( 'image'== this.aItems[i].sType ) {
			document.getElementById( 'c_' + this.aItems[i].sId ).style.display = 'none';

			__oContainer = document.getElementById( 'c_' + this.aItems[i].sId );
			oImageA = __oContainer.getElementsByTagName('a')[0];
			//oImageA.className = 'thickbox';
			if( oImageA ) oImageA.className = 'no_thickbox';

		}

		document.getElementById( 'i_' + this.aItems[i].sId ).className = document.getElementById( 'i_' + this.aItems[i].sId ).className.replace("selected", "");
		if ( sId==this.aItems[i].sId ) this.oCurrent = this.aItems[i];
	}

	if ( this.oCurrent ) {

		document.getElementById( 'i_' + this.oCurrent.sId ).className+=" selected";
		if ( 'video'==this.oCurrent.sType ){
			document.getElementById( this.sId + '_player' ).innerHTML = this.oCurrent.sContent;
			document.getElementById( this.sId + '_player' ).style.display = 'block';
		}else if ( 'image'==this.oCurrent.sType ){
			oContainer = document.getElementById( 'c_' + this.oCurrent.sId );
			oImage = oContainer.getElementsByTagName('img')[0];
			oImage.src = this.oCurrent.sContent;
			oContainer.style.display = 'block';
		}

	    if ( this.oCurrent.sUrl ) {
	       //daca elementul are deja display none, atunci va ramane asa (pt galeria foto la elementul <p>)
	       if ( document.getElementById( this.sId + '_description' ).style.display == "none" )
	          document.getElementById( this.sId + '_description' ).style.display == "none";
	       else
	          document.getElementById( this.sId + '_description' ).style.display = "block";

	       document.getElementById( this.sId + '_description' ).innerHTML = "<a href= " + this.oCurrent.sUrl + ">" + this.oCurrent.sDescription + "</a>";

	    }
	    else if ( this.oCurrent.sDescription ) {
	       //daca elementul are deja display none, atunci va ramane asa (pt galeria foto la elementul <p>)
	       if ( document.getElementById( this.sId + '_description' ).style.display == "none" )
	          document.getElementById( this.sId + '_description' ).style.display == "none";
	       else
		      document.getElementById( this.sId + '_description' ).style.display = "block";

		   document.getElementById( this.sId + '_description' ).innerHTML = this.oCurrent.sDescription;

		}
		else if ( this.oCurrent.sDescription === "" )
		   //ascunde <div class="descriereVideo">
		   document.getElementById( this.sId + '_description' ).style.display = "none";
	}

}

function FPGalleryItem( sType, sId, sContent, sDescription, sUrl ){
	this.sType = sType;
	this.sId = sId;
    this.sContent = sContent;
    this.sDescription = sDescription;
    this.sUrl = sUrl;
}

function MidnightCaller(){
	this.aCalls = new Array();
}

MidnightCaller.prototype.add = function ( sFunc, sArgs ){
	this.aCalls.push( new Array( sFunc, sArgs ) );
}

MidnightCaller.prototype.callall = function (){
	for (var i = 0; i < this.aCalls.length; i++){
		_aCall = this.aCalls[i];
		eval( _aCall[0] + "(" + _aCall[1] + ")" );
	}
}

var oMidnightMan = new MidnightCaller();


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* convertor valutar */
var moneda2=1;

function cursValutar(id){

	if(id==1)
		moneda1=document.getElementById("moneda1").value;
	else if(id==2)
		moneda2=document.getElementById("moneda2").value;

	conversieValuta();
}


function verificareNumar(){

	var value    = document.getElementById('suma').value;
	var expresie = /(^\d+$)|(^\d+\.\d+$)/

	if (expresie.test(value))
		verificare=true
	else{
		verificare=false
	}

	return (verificare)
}



function conversieValuta(){

	var data = jQuery('#data').val();
	var cdata = jQuery('#cdata').val();

	if(data == cdata){


	    if (verificareNumar()){

		var moneda1=document.getElementById("moneda1_convert").value;
		var moneda2=document.getElementById("moneda2_convert").value;

		var suma        = document.getElementById('suma').value;

		var element1    = document.getElementById('moneda1_convert');
		    var moneda1text = element1.options[element1.selectedIndex].text
		    var element2    = document.getElementById('moneda2_convert');
		    var moneda2text = element2.options[element2.selectedIndex].text

		if(aMultiplier[moneda1text]!='1' && aMultiplier[moneda2text]=='1')
		       var rezultat    = Math.round(10000*suma*moneda1/moneda2/aMultiplier[moneda1text])/10000;
		else if(aMultiplier[moneda2text]!='1' && aMultiplier[moneda1text]=='1')
		       var rezultat    = Math.round(10000*suma*moneda1/moneda2*aMultiplier[moneda2text])/10000;
		else
		       var rezultat    = Math.round(10000*suma*moneda1/moneda2)/10000;

		setResult( rezultat );

	    }
	}else{
	    var element1    = document.getElementById('moneda1_convert');
	    var moneda1text = element1.options[element1.selectedIndex].text
	    var element2    = document.getElementById('moneda2_convert');
	    var moneda2text = element2.options[element2.selectedIndex].text
	    var suma        = document.getElementById('suma').value;

	    convertRemote(data, suma, moneda1text, moneda2text);
	}
}

$(document).ready(function(){
    $('#submit_btn').click(function(ev){
	ev.preventDefault();
	conversieValuta();
    });
});


function setResult( rezultat ){
    jQuery('#rezultat').val( rezultat.toFixed(3) );
    var rezultattva = rezultat * 1.24;
    jQuery('#rezultattva').val( rezultattva.toFixed(3) );
}

function convertRemote( data, suma, moneda1text, moneda2text ){
    $.ajax({
	type: "POST",
	url: "/charts/convertor.php",
	data: "data=" + data + "&m1=" + moneda1text + "&m2=" + moneda2text + "&suma=" + suma,
	async:false,
	//dataType: "json",
	success: function(resp){
	   setResult( parseFloat( resp ) );
	}
    });
}



// calculator TVA
function calculatorTVA(tip, tva){

    var tva = document.getElementById('tva').value;

    if(tip == '1' && document.getElementById('camp1').value != 'net') {
        document.getElementById('camp2').innerHTML = (tva/100 * document.getElementById('camp1').value).toFixed(2);
        document.getElementById('camp3').value     = ((tva/100+1) * document.getElementById('camp1').value).toFixed(2);
    }

    else if(tip == '2') {
        document.getElementById('camp1').value     = (100 * document.getElementById('camp3').value / (parseInt(tva)+100)).toFixed(2);
        document.getElementById('camp2').innerHTML = (tva/100 * document.getElementById('camp1').value).toFixed(2);
    }

}

function resetTVA() {

    var camp1 = document.getElementById('camp1');
    var camp2 = document.getElementById('camp2');
    var camp3 = document.getElementById('camp3');

    if (camp1.value==null||camp1.value=='') camp1.value = 'net';
    if (camp3.value==null||camp3.value=='') camp3.value = 'total';
    if (camp1.value=='net'||camp3.value=='total') camp2.innerHTML = '-';

}

// limitare text comentarii
function commentCounter(comment, cntComment, limit) {

    if (comment.value.length > limit)
        comment.value = comment.value.substring(0, limit);
    else
        cntComment.value = limit - comment.value.length;
}


delta_font_size = 0;
content_font_size = 0;
default_font_size = 13;
default_line_height = 17;

function change_font_size( container, _delta ){

	_element = document.getElementById( container );
	if ( null == _element ) return;

	delta_font_size+= _delta;

	if ( delta_font_size==-default_font_size ){

		delta_font_size-= _delta;

		return;

	}

	content_font_size = default_font_size + delta_font_size;
	content_line_height = Math.round( content_font_size * default_line_height / default_font_size );

	_element.style.fontSize = content_font_size + "px";
	_element.style.lineHeight = content_line_height + "px";

}

function rank_over( iRank, iId ){
	for(i=1;i<=iRank;i++){
		document.images['star' + iId + '_' + i].src = _preload.src;
	}
}

function rank_out( iCurrentRank, iId ){
	for(i=1;i<=iMaxRank;i++){
		document.images['star' + iId + '_' + i].src = ( i<=iCurrentRank ? sStarOn : sStarOff );
	}
}

function rank_it( sModule, iId, iRank ){

	xajax_rank( sModule, iId, iRank );
}


var btn_close = '/templates/default/images/close.jpg';
function popup_show( _title, _width, _height, _content ) {

	$('#popup').html( '<div class="popupContent"><a onclick="popup_close(0)" href="javascript:void(0)" style="float:right"><img src="'+ btn_close +'" alt=""></a><p id="popup-title" class="popupTitle"></p><div id="popup-content"></div><div style="height: 1px; clear: both;"></div></div>' );
	if ( _title ) $('#popup-title').html( _title );
	if ( _content ) $('#popup-content').html( _content );
	if ( _width ) $('#popup').width( _width );
	if ( _height ) $('#popup').height( _height );


	$('#popup').center();
	$('#popup').fadeIn('slow');

}

function popup_close( _duration ){

	if ( $('#popup-overlay').length ){
		$('#popup-overlay').hide();
	}

	$('#popup').css('height', 'auto');
	$('#popup-title').html( '' );
	$('#popup-area').html( '' );

	if ( !_duration ){
		$('#popup').hide();
	}else{
		$('#popup').fadeOut( _duration );
	}
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

jQuery(document).ready(function(){
    //change video
    jQuery('.playVideo').live('click',function(ev){
	ev.preventDefault();
	var vidId = $(this).attr('id');
	var url = getBaseURL() ; //TO DO: de modificat pe versiunea finala
	$.ajax({
	    type: "POST",
	    url: url + "videox.php",
	    data: "do=getVideo&iId=" + vidId,
	    async:false,
	    dataType: "json",
	    success: function(resp){ //alert(resp);

			jQuery("#videoGalleryBig").html(resp.video);

			$('.galerieVideo .ad-gallery').adGallery();


			el = $('.galerieVideo ul.ad-thumb-list li:first a');
			showVideo(el);

			if(jQuery('html, body').scrollTop() > 260)
				jQuery('html, body').animate({scrollTop:0}, 'slow');
			}
		});
    });

    //next videos
    jQuery('.nextVideos').live('click',function(ev){
	ev.preventDefault();
	getBatch();
    });


    //navigation
    jQuery("#nav .pagination a").live('click',function(ev){
	ev.preventDefault();
	if( jQuery(this).attr('class') == 'fw' ){
	    var pag = parseInt($(this).prev('a').html()) + 1;
	    if( isNaN(pag) ){
		var pag = parseInt($(this).prev('span').html()) + 1;
	    }
	}else if( jQuery(this).attr('class') == 'bk' ){
	    var pag = parseInt($(this).next('a').html()) - 1;
	    if( isNaN(pag) ){
		var pag = parseInt($(this).next('span').html()) - 1;
	    }
	}else{
	    var pag = parseInt(jQuery(this).html());
	}

	if(!isNaN(pag)){
	    var oldActive = parseInt( $(this).siblings('span').html() );
	    $(this).siblings('span').replaceWith('<a title="pagina ' + oldActive +' din '+ oldActive + '" href="/multimedia/foto/&p='+ oldActive +'">' + oldActive + '</a>');
	    $(this).replaceWith('<span>'+ pag +'</span>');
	    $('#page').val(pag - 1);
	    $('#offset').val('0');
	    getBatch();
	}

    });


    //change foto
    jQuery('.getFotoGallery').live('click',function(ev){
	ev.preventDefault();
	var fotoId = $(this).attr('id');
	var url = getBaseURL(); //TO DO: de modificat pe versiunea finala
	$.ajax({
	    type: "POST",
	    url: url + "videox.php",
	    data: "do=getFoto&iId=" + fotoId,
	    async:false,
	    dataType: "json",
	    success: function(resp){ //alert(resp.toSource());
			jQuery("#fotoGalleryBig").html(resp.foto);

			$('.galerieFoto .ad-gallery').adGallery();

			if(jQuery('html, body').scrollTop() > 260)
				jQuery('html, body').animate({scrollTop:0}, 'slow');
			}

		});
    });


});

function getBatch(){
    //loading(true); //set loader animation
    var vidPage 	= parseInt(jQuery('#page').val()) + 1;
    var url = getBaseURL() ; //TO DO: de modificat pe versiunea finala

    if(jQuery("#foto").val() == '1'){
	//set offset for fisrt page
	/*if(vidPage > 1)
	    $('#offset').val('0');
	    else if(vidPage == 1)
		$('#offset').val(jQuery('#originaloffset').val());*/
	var firstOffset 	= parseInt(jQuery('#originaloffset').val());

	$.ajax({
	    type: "POST",
	    url: url + "videox.php",
	    data: "do=getFotoBatch&vidPage=" + vidPage + "&firstOffset=" + firstOffset,
	    async:false,
	    dataType: "json",
	    success: function(resp){  //alert(resp.toSource());
		//set html code
		jQuery("#fotoGalleryThumb").html(resp.output);

		//loading(false); //unset loader animation

		jQuery('#page').val(parseInt(jQuery('#page').val()) + 1);
	    }
	});
    }else{
	//var vidCat 		= jQuery('li[class="on"]').attr('id') | 0;
	//set offset for fisrt page and no category
	/*if(vidPage > 1)
	    $('#offset').val('0');
	    else if(vidPage == 1 && vidCat == 0)
		$('#offset').val(jQuery('#originaloffset').val());*/
	var firstOffset 	= parseInt(jQuery('#originaloffset').val());

	$.ajax({
	    type: "POST",
	    url: url + "videox.php",
	    data: "do=getBatch&vidPage=" + vidPage + "&firstOffset=" + firstOffset,
	    async:false,
	    dataType: "json",
	    success: function(resp){  //alert(resp.pag.toSource());
		//set html code
		jQuery("#videoGalleryThumb").html(resp.output);

		//loading(false); //unset loader animation

		jQuery('#page').val(parseInt(jQuery('#page').val()) + 1);
	    }
	});
    }
}

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

function getCookie( c_name ){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++){
	x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	x=x.replace(/^\s+|\s+$/g,"");
	if (x==c_name){
	    return unescape(y);
	}
    }
}

function checkCookie( c_name ){
    var cookie = getCookie( c_name );
    if (cookie!=null && cookie!=""){
	return true;
    }else{
	return false;
    }
}
