function openVerMenu(mid){
	if($("#vsubmenu_"+mid).css("display")=="none"){
		$("#vsubmenu_"+mid).show(200);
		$("#vmenu_group_"+mid).css("background","#f1f4f0");
		saveOpenedMenu(mid,1);
	}else{
		$("#vsubmenu_"+mid).hide(200);
		$("#vmenu_group_"+mid).css("background","none");
		saveOpenedMenu(mid,0);
	}
}
function saveOpenedMenu(mid,stat){
	 $.ajax({
	   type: "get",
	   url: saveMenuScript,
	   data: "mid="+mid+"&stat="+stat,
	   success: function(msg){
		 void(0);
	   }
	 });
}

window.onload=function() {
	$('input[type=text][title!=""]').each(function() {
	if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
	if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
	}).focus(switchText).blur(switchText);
	/*$('a[rel=lightbox]').each(function(){
		$(this).attr('href',$(this).attr('href')+screenSize+'.jpg');
	}).lightBox();*/
	$('a[rel=lightbox]').each(function(){
		$(this).attr('href',$(this).attr('href')+screenSize+'.jpg');
	}).fancybox({ 'titlePosition':'over', 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
	slidePromo('slidingPromo',5000,1000);
	quotes();
	commentList();
	showTopMenuSub();
}

function ajax_sendnews(link, container, value) {
	if(value != ''){
		$.ajax({
			url : link,
			data : { email:value,action:'asdsa' },
			type : 'post',
			success : function(msg){
				$('#'+container).html(msg).fadeIn(100);
				$('#'+container).fadeOut(6000);
			}
		});
	}
}
function switchText(){
	if ($(this).val() == $(this).attr('title'))
		$(this).val('').removeClass('exampleText');
	else if ($.trim($(this).val()) == '')
		$(this).addClass('exampleText').val($(this).attr('title'));
}

function showDiv(id,hide){
	if(hide == 1){
		$('div.indexForm').each(function(){
			if($(this).css('display') != 'none') $(this).hide(500);
		});
	}
	div = $('#'+id);
	if(div.css('display') == 'none') div.show(500);
	else div.hide(500);
	return false;
}

function showTopMenuSub(){
	$('li.mainLevelLi').each(function(){
		var div = $($(this).children('div').children('ul'));
		$(this).bind("mouseenter",function(){
			div.show();	
			$(this).addClass('openMenu');
		}).bind("mouseleave",function(){
			div.hide();
			$(this).removeClass('openMenu');
		});
	});
	$('ul.topMenusub li').each(function(){
		var div = $($(this).children('ul'));
		$(this).bind("mouseenter",function(){
			div.show();	
			$(this).addClass('openMenu');
		}).bind("mouseleave",function(){
			div.hide();
			$(this).removeClass('openMenu');
		});
	});
	return false;
}

function slidePromo(sClass,timer,fadeTimer){
	actionSlide(sClass,timer,fadeTimer);
}

function actionSlide(sClass,timer,fadeTimer) {
	$('div.'+sClass).each(function(i){
		j=i+1;
		$(this).oneTime(j*timer,function(){
			$(this).fadeOut(fadeTimer);
		});
		if($('div.'+sClass).length-1 == i){
			$('div.'+sClass+':first').oneTime(j*timer,function(){
				$(this).fadeIn(fadeTimer);
				setTimeout(function(){
					slidePromo(sClass,timer,fadeTimer);
				},fadeTimer);
			}).stop();
		}else{
			$(this).next().oneTime(j*timer,function(){
				$(this).fadeIn(fadeTimer);
			});
		}
	});  
}

function quotes(){
	$('span.quotes').each(function(){
		var html = $(this).html();
		$(this).html('').append('<div class="quotesBg"><span class="quotesBg">'+html+'</span></div>');
	}).after('<div>&nbsp;</div>');
}

function commentList(offset){
	var offset = (offset)?offset:0;
	if(blogID > 0){
		$('#commentListBoxLoading').show();
		$('#commentListBoxAjax').each(function(){
			var div = $(this);
			$.ajax({
				url:	commentLink,
				type:	"post",
				data:	{id:blogID, action:'getComment', offset:offset},
				success:	function(msg){
					div.html(msg);
					$('#commentListBoxLoading').hide();
				}
			});
		});
	}	
}

function commentListDelete(id,type){
	if(confirm('Slet?')){
		if(id > 0 && type == ''){
			$('#commentListBoxLoading').show();
			var div = $(this);
			$.ajax({
				url:	commentLink,
				type:	"post",
				requestHeaders: {Accept: 'application/json'},
				data:	{id:id, action:'deleteComment'},
				success:	function(msg){
					var JSONtext = msg;
					var answer = JSON.parse(JSONtext);
					if(answer.success == 1){
						$('#countCommentID').html(parseFloat($('#countCommentID').html())-1);
						commentList(0);
					}
				}
			});
		}else{
			$('#commentListBoxLoading').show();
			$.ajax({
				url:	commentLink,
				type:	"post",
				requestHeaders: {Accept: 'application/json'},
				data:	{id:id, action:'deleteCommentAdmin'},
				success:	function(msg){
					var JSONtext = msg;
					var answer = JSON.parse(JSONtext);
					if(answer.success == 1){
						commentList(0);
					}
				}
			});
		}
	}
}
function checkformqcomment ( form ){	
	var error=0;
	var name=trimAll($('#com_inp_name').attr('value'));
	var comments=trimAll($('#com_inp_comment').attr('value'));
	var mailreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var email=trimAll($('#com_inp_email').attr('value'));
	var city=trimAll($('#com_inp_city').attr('value'));
	
	if (name == ''){ error++;$('#Lcom_inp_name').attr('class',"sendtext_error"); 
	}else $('#Lcom_inp_name').removeAttr('class',"sendtext_error");
	if (city == ''){ error++;$('#Lcom_inp_city').attr('class',"sendtext_error"); 
	}else $('#Lcom_inp_city').removeAttr('class',"sendtext_error");
	if (comments == ''){ error++;$('#Lcom_inp_comment').attr('class',"sendtext_error");
	}else $('#Lcom_inp_comment').removeAttr('class',"sendtext_error");
	if (mailreg.test(email) == true) $('#Lcom_inp_email').removeAttr('class',"sendtext_error");
	else{ error++;$('#Lcom_inp_email').attr('class',"sendtext_error"); }	
	
	if(error != 0){
		$('#formError').fadeIn(100).fadeOut(3000);
	}else{
		$('#commentAddAjax').show();
		$.ajax({
			url:	commentLink,
			type : "post",
			requestHeaders: {Accept: 'application/json'},
			data: {id:blogID, name: name, comment: comments, city: city, email: email, action: 'commentFormSend'},
			success: function(msg) {
				var JSONtext = msg;
				var answer = JSON.parse(JSONtext);
				$('#commentAddAjax').hide();
				if(answer.success == 1){
					$('#commentListBox').show();
					//$('#countCommentID').html(parseFloat($('#countCommentID').html())+1);
					$('#formSuccess').fadeIn(100).fadeOut(3000);
					$('#f_comment :input').each(function(){
						$(this).val('');
					});
					commentList(0);
				}else $('#formError').fadeIn(100).fadeOut(3000);
			}
		});
	}
	return false;
}

function adminComment(form, id){
	$('#commentAddAjaxAdmin').show();
	var comments = $('#'+form+' :input:first').val();
	$.ajax({
		url:	commentLink,
		type : "post",
		requestHeaders: {Accept: 'application/json'},
		data: {pID:blogID, id:id, comment: comments, action: 'commentAdminFormSend'},
		success: function(msg) {
			var JSONtext = msg;
			var answer = JSON.parse(JSONtext);
			$('#commentAddAjaxAdmin').hide();
			if(answer.success == 1){
				$('#'+form+' :input').each(function(){
					$(this).val('');
				});
				commentList(0);
			}else{
				commentList(0);
			}
		}
	});
}

function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;
	if(objRegExp.test(strValue)) {
		 strValue = strValue.replace(objRegExp, '');
		 if( strValue.length == 0)
			return strValue;
	}
	 objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	 if(objRegExp.test(strValue)) {
		 //remove leading and trailing whitespace characters
		 //strValue = strValue.replace(objRegExp, '$2');
	}
	return strValue;
}





/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/10/16
 *
 * @author Blair Mitchelmore
 * @version 1.2
 *
 **/

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval < 0)
				return;

			if (typeof times != 'number' || isNaN(times) || times < 0) 
				times = 0;
			
			times = times || 0;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});
