var timeout    = 500;
		var closetimer = 0;
		var ddmenuitem = 0;
		
		function ddmenu_open(){
			ddmenu_canceltimer();
		   	ddmenu_close();
		   	ddmenuitem = $(this).find('ul').css('visibility', 'visible');
		}
		
		function ddmenu_close(){ 
			if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
		}
		
		function ddmenu_timer(){
			closetimer = window.setTimeout(ddmenu_close, timeout);
		}
		
		function ddmenu_canceltimer(){  
			if(closetimer){  
				window.clearTimeout(closetimer);
		        closetimer = null;
		}}

$(document).ready(function () 	{	
	$(document).pngFix(); 
	//$('.menu > li').bind('mouseover', ddmenu_open)
	//$('.menu > li').bind('mouseout',  ddmenu_timer)
	
	$("#submit-comment").validationEngine();
	$("#submit-alarma").validationEngine();
	
	$(".slider").jCarouselLite({
		btnNext: "#navright",
		btnPrev: "#navleft",
		visible: 6
	});
	
	//report comments
	var report='api.php?comment=reported';
		 
		 $("button.report").click(function(){ 		
			comID=$(this).val(); 		
			$.post(report,{id:comID},function(response){
				$("#" + comID + "").html(response); 
			});
			$("button.report " + comID + "").attr({"disabled":"disabled"});
		});
	//end report comments
	
	//report alarm
	var reportal='api.php?alarm=reported';
		 
		 $("button.reporta").click(function(){ 		
			alID=$(this).val(); 		
			$.post(reportal,{id:alID},function(response){
				$("#" + alID + "").html(response); 
			});
			$("button.reporta " + alID + "").attr({"disabled":"disabled"});
		});
	//end report alarm
	
	//report obyavi
	var reportad='api.php?ad=reported';
		 
		 $("button.reportad").click(function(){ 		
			adID=$(this).val(); 		
			$.post(reportad,{id:adID},function(response){
				$("#" + adID + "").html(response); 
			});
			$("button.reportad " + adID + "").attr({"disabled":"disabled"});
		});
	//end report obyavi
	
	//valarma limit simvoli
	/*$('#al').keyup(function() {
                var len = this.value.length;
                if (len > 300) {
                    this.value = this.value.substring(0, 300);
                }
                //$('#charLeft').text(3 - len);
            });*/
	//valarma limit simvoli
	
	//$(".tabs-nav-inner").jCarouselLite({
	//btnNext: ".tabsright",
	//btnPrev: ".tabsleft", 
	//visible: 4
	//});
	
	$( "#tabs" ).tabs({ event: "mouseover" });
	$("div.center").css("display", "block");

});

$(document).ready(function() {
						   
	// Here we will write a function when link click under class popup				   
	$('a.popup').click(function() {
										
										
	// Here we will describe a variable popupid which gets the
	// rel attribute from the clicked link							
	var popupid = $(this).attr('rel');
	
	
	// Now we need to popup the marked which belongs to the rel attribute
	// Suppose the rel attribute of click link is popuprel then here in below code
	// #popuprel will fadein
	$('#' + popupid).fadeIn();
	
	
	// append div with id fade into the bottom of body tag
	// and we allready styled it in our step 2 : CSS
	$('body').append('<div id="fade"></div>');
	$('#fade').css({}).fadeIn();
	$('.formError').css({}).fadeIn();
	
	
	// Now here we need to have our popup box in center of 
	// webpage when its fadein. so we add 10px to height and width 
	var popuptopmargin = ($('#' + popupid).height() + 10) / 2;
	var popupleftmargin = ($('#' + popupid).width() + 10) / 2;
	
	
	// Then using .css function style our popup box for center allignment
	$('#' + popupid).css({
	'margin-top' : -popuptopmargin,
	'margin-left' : -popupleftmargin
	});
	});
	
	
	// Now define one more function which is used to fadeout the 
	// fade layer and popup window as soon as we click on fade layer
	$('#fade').click(function() {
	
	
	// Add markup ids of all custom popup box here 						  
	$('#fade , #login , .formError ').fadeOut()
	return false;
	});
});

document.onclick = ddmenu_close;

/*-----------CURRENCY WIDGET-------------*/
function loadCER(){
    jQuery.ajax({
        url: 'cerDataFeeder.php',
        type: "GET",
        dataType: 'json',
        error: function(){},
        success: function(data){
            var domId = '';
            jQuery.each(data, function(){
                jQuery('#'+this["@attributes"]["currency"]+' > .country').append(this["@attributes"]["currency"]);
                jQuery('#'+this["@attributes"]["currency"]+' > .rate').append(this["@attributes"]["rate"]);
            });
            
        }
    });
}

function convertCER(){
    jQuery('.currency > li').hover(function(){
        
        var liId = jQuery(this).attr('id');
        var crate = parseFloat(jQuery(liId).text());
        var selectedCRate =  jQuery(this).find('.rate').text();
        var _crate = 0;
        var _crateConverted = 0;
        
        jQuery(".currency > li").each(function(){
            
            _crate = parseFloat(jQuery(this).find('.rate').text());
            _crateConverted = _crate/selectedCRate;
            jQuery(this).find('.rate').text(_crateConverted.toFixed(4));
            
        });
        jQuery(this).find('.rate').text('1.0000');
    });
}
/*-----------WEATHER WIDGET / PAGE
 
08491828ce122742110906 - Varna key
57f2afec58080533110709 - Burgas key
9e85d42189104946111710 - Plovdiv key

-------------*/

//var urlWeatherWidget = 'http://free.worldweatheronline.com/feed/weather.ashx?q=varna,bulgaria&format=json&num_of_days=3&key=08491828ce122742110906';
//var weatherPageVarnaUrl = 'http://free.worldweatheronline.com/feed/weather.ashx?q=varna,bulgaria&format=json&num_of_days=4&key=08491828ce122742110906';

var weather = {
	
	buildUrls: function(key, location) {
		
		var urlWeatherWidget = 'http://free.worldweatheronline.com/feed/weather.ashx?q='+location+'&format=json&num_of_days=3&key='+key;
		var weatherPageVarnaUrl = 'http://free.worldweatheronline.com/feed/weather.ashx?q='+location+'&format=json&num_of_days=4&key='+key;
		
	},
	
	buildCurentConditions: function(data){
	    jQuery('.cw-image').append('<img src="css/images/weathericons/large/'+getImageName(data.weatherCode)+'.png">');
	    jQuery('.cw-temp').append(data.temp_C+'°C');
	    jQuery('.cw-update > span').append(data.observation_time);
	    jQuery('.cw-visibility > p').append(data.visibility+' км');
	    jQuery('.cw-humidity > p').append(data.humidity+' %');
	    jQuery('.cw-wind > p').append(data.windspeedKmph+' км/ч');
	    jQuery('.cw-pressure > p').append(data.pressure+' hPa');
	},
    
	
	buildNextFourDays: function(data){
	    var domId = '';
	    jQuery.each(data, function(i, val){
		domId='#day'+i;
		jQuery(domId+' > ul > .fc-date').append(jQuery.format.date(val.date+' 00:00:00.000', 'dd - MMM'));
		jQuery(domId+' > ul > .fc-icon').append('<img src="css/images/weathericons/'+getImageName(val.weatherCode)+'.png">');
		jQuery(domId+' > ul > .fc-temp').append(val.tempMinC+'/'+val.tempMaxC+'°C');
	    });
	},
	
	buildCitiy: function(data, city){
		jQuery('#'+city.name+' > .fcc-icon')
		    .append('<img src="css/images/weathericons/'+getImageName(data.weatherCode)+'.png">');
		jQuery('#'+city.name+' > .fcc-temp')
		    .append(city.namebg+' '+data.tempMinC+'/'+data.tempMaxC+'°C');
	},
	
	buildWidget: function(current_forcast, forcast) {
		
	    var domId = '';
	    
	    jQuery('.cw-image-main').append('<img src="css/images/weathericons/main/'+getImageName(current_forcast.weatherCode)+'.png">');
	    jQuery('.cw-temp-main').append(current_forcast.temp_C+'°');
	    
	    jQuery.each(forcast, function(i, val){
		domId='#day'+i;
		jQuery(domId+' > .deg').append(val.tempMaxC+'°C');
		if (domId !== '#day0') {jQuery(domId+' > .day').append(jQuery.format.date(val.date+' 00:00:00.000', 'ddd'));}
		jQuery(domId+' > .icon').append('<img src="css/images/weathericons/'+getImageName(val.weatherCode)+'.png">');
	    });
    
	}
}

function loadWeatherWidget(location, key){
    
    var urlWeatherWidget = 'http://free.worldweatheronline.com/feed/weather.ashx?q='+location+'&format=json&num_of_days=3&key='+key;
    jQuery.ajax({
	url: urlWeatherWidget,
	dataType: 'jsonp',
	success: function(data){
	    weather.buildWidget(data.data.current_condition[0], data.data.weather);
	    
	    //jQuery.JSONCookie(name, data, {expires : exp_date, path: '/'});
	}
    });
    
    //var name = 'vutr_weather_';
    //var weather_obj = null;
    //var date = new Date();
    ////var exp_date = Math.round(date.getTime() / 1000);
    //var exp_date = Math.round(date.getTime() / 1000) + (60 * 15 * 1);
    //weather_obj = jQuery.JSONCookie(name);
    //
    //if (weather_obj.data) {
    //    weather.buildWidget(weather_obj.data.current_condition[0], weather_obj.data.weather);
    //} else {
    //    jQuery.ajax({
    //        url: urlWeatherWidget,
    //        dataType: 'jsonp',
    //        success: function(data){
    //            weather.buildWidget(data.data.current_condition[0], data.data.weather);
    //            
    //            jQuery.JSONCookie(name, data, {expires : exp_date, path: '/'});
    //        }
    //    });
    //}
}


function loadWeatherPageVarna(location, key){
	
	var weatherPageVarnaUrl = 'http://free.worldweatheronline.com/feed/weather.ashx?q='+location+'&format=json&num_of_days=4&key='+key;
	
	jQuery.ajax({
		url: weatherPageVarnaUrl,
		dataType: 'jsonp',
		success: function(data){
		    weather.buildCurentConditions(data.data.current_condition[0]);
		    weather.buildNextFourDays(data.data.weather);
		}
	    });
	
	//var name = 'vutr_weather_page_';
	//var weather_obj = null;
	//var date = new Date();
	//
	//weather_obj = jQuery.JSONCookie(name);
	//
	//if (weather_obj.data) {
	//	weather.buildCurentConditions(weather_obj.data.current_condition[0]);
	//	weather.buildNextFourDays(weather_obj.data.weather);
	//} else {
	//	jQuery.ajax({
	//	    url: weatherPageVarnaUrl,
	//	    dataType: 'jsonp',
	//	    success: function(data){
	//		weather.buildCurentConditions(data.data.current_condition[0]);
	//		weather.buildNextFourDays(data.data.weather);
	//		var exp_date = date.getTime() + (1000 * 60 * 60 * 2);
	//		jQuery.JSONCookie(name, data, {expires : exp_date, path: '/'});
	//	    }
	//	});
	//
	//}
}

 
function loadWeatherPageCities(){
    var cities = [
        {country: 'bulgaria', name: 'sofia', namebg: 'София'},
        {country: 'bulgaria', name: 'plovdiv', namebg: 'Пловдив'},
        {country: 'bulgaria', name: 'burgas', namebg: 'Бургас'},
        {country: 'bulgaria', name: 'varna', namebg: 'Варна'},
        {country: 'bulgaria', name: 'ruse', namebg: 'Русе'},
        
        {country: null, name: 'london', namebg: 'Лондон'},
        {country: null, name: 'rome', namebg: 'Рим'},
        {country: null, name: 'moscow', namebg: 'Москва'},
        {country: null, name: 'paris', namebg: 'Париж'},
        
        {country: null, name: 'Dubai', namebg: 'Дубай'},
        {country: null, name: 'chicago', namebg: 'Чикаго'},
        {country: null, name: 'tokyo', namebg: 'Токио'},
        {country: null, name: 'sydney', namebg: 'Сидни'}
    ];
    
    var urlWeatherPageCities = 'http://free.worldweatheronline.com/feed/weather.ashx?q=';
    var url = '';
    
    jQuery.each(cities, function(i, city){
    
        if(city.country) url = urlWeatherPageCities + city.name + ',' + city.country + '&format=json&num_of_days=4&key=08491828ce122742110906';
        else url = urlWeatherPageCities + city.name + '&format=json&num_of_days=4&key=08491828ce122742110906';
        
        jQuery.ajax({
            url: url,
            dataType: 'jsonp',
            success: function(data){
                weather.buildCitiy(data.data.weather[0], city);
            }
        });
    });
}

function getImageName(weatherCode){
        var fog = [260,248,143];  //34
        var lrain = [296,266,185,182,176,355,353,302,356, 293, 263, 299]; //39
        var hraint = [389,200,386,308]; //0
        var hsnow = [230,227]; //14
        var rainsnow = [320,362,365,317,314,311,284,281]; //7
        var lsnow = [329,323,179,335]; //16
        var hsnow = [371,368,332];//43
        var pellets = [377,350];//25
        var clear = [113];//36
        var cloudy = [116];//30
        var hcloudy = [119];//48
        var overcast = [122,326];//26
        var codesArr = new Array(fog, lrain, hraint, hsnow, rainsnow, lsnow, hsnow, pellets, clear, cloudy, hcloudy, overcast);
        var codesNames = new Array(34, 39, 0, 14, 7, 16, 43, 25, 36, 30, 48, 26);
        
        for(var i in codesArr){
                if(jQuery.inArray(parseInt(weatherCode),codesArr[i]) > -1) {
                        var imageNum = codesNames[i];
                        break;
                } else { continue; }
        }
        
        return imageNum;
}

