var main = {
	domain:'',
	lang:false,

	toolz:function(id,text,t,l,c,e){
		if(!id){id="a";}
		if(!t){t=15;}else{t=parseInt(t);}
		if(!l){l=15;}else{l=parseInt(l);}
		if(!c){c='';}
		if(!e){e='p';}
		if(!text){
			$(id).tooltip({
				track: true,
				delay: 0,
				showURL: false,
				showBody: " - "
			});
		}else{
			$(id).tooltip({
				track: true,
				delay: 0,
				showURL: false,
				showBody: " - ",
				top:t,
				left:l,
				extraClass:c,
				bodyHandler:function(){
					return $("<"+e+"/>").html(text);
				}
			});
		}
	},

	shadow:function(id,p,col){
		if(!col){col = '#FFFFFF';}
		if(!p){p = 0;}
		$('#shadow_'+id).remove();	
		if(p!=1){			
			$('#'+id).before($('<table/>').attr({'id':'shadow_'+id,'class':'shadows'}).html($('<tr/>').html($('<td/>').html(''))));
			$('#shadow_'+id).css({'width':$('#'+id).width(),'height':$('#'+id).height(),'opacity':0.35,'background-color':col})
			.animate({'opacity':0.95},5000);
		}
	},

	recode:function(){
		var date = new Date();
		var time = date.getTime();
		var src = '/code.php?'+time;
		$('#codepic').attr({'src':src});
	},
	
	slideMenu:function(id){
		if(!id){id='';}		
		$(id + 'a.slider').click(function(){
			var id = $(this).attr('id');
		
			if($('#'+id+'s').css('display')=='none'){
				$('#'+id+'s').show();	
				$('#'+id+' img.slider').attr({'src':main.domain+'/img/arrowopen.png'});	
			}else{
				$('#'+id+'s').hide();			
				$('#'+id+' img.slider').attr({'src':main.domain+'/img/arrow.png'});						
			}
			return false;	
		});
	
	},
		
	set:function(domain){
		main.domain = domain;
		main.url_reload(0);
		main.toolz();	
		main.input_value();
		main.slideMenu();
	},
	
	q_save:{},
	input_value:function(f){
		if(typeof f == 'undefined'){f = 'input.sgray,textarea.sgray';}
		$(f).each(function(){main.q_save[$(this).attr('id')] = $(this).val();})
		.bind('focus',function(){if($(this).val() == main.q_save[$(this).attr('id')]){$(this).val('');$(this).removeClass('sgray');if($(this).attr('id')=='q'){$('#search_btn').show();}}})
		.bind('blur',function(){if($(this).val() == main.q_save[$(this).attr('id')] || $(this).val().length == 0){$(this).addClass('sgray').val(main.q_save[$(this).attr('id')]);if($(this).attr('id')=='q'){setTimeout("$('#search_btn').hide();",100);}}});
	},
	
	chekWindowType:function(){	
		var wo = window.parent.document.getElementById('iframebox');
		var wos = window.parent.document.body;		
		if(wos && wo && window.parent !== window){
			popbox.res();		
		}	
	},

	field_counting:{},
	field_len:function(id,len,c){		
		len = parseInt(len);
		if(typeof c == 'undefined'){c = false;}
		if(typeof main.field_counting[id] == 'undefined' || c){		
			main.field_counting[id] = len;
			main.startCount('',id);
			$("#"+id).bind('change keyup keydown',{'id':id},main.startCount);
		}else{
			$("#"+id).unbind('change keyup keydown',main.startCount);
		}
		return false;
	},
	startCount:function(e,ids){
		if(!ids){
			var id = e.data.id;
		}else{
			var id = ids;
		}
		setTimeout(function(){
			if(typeof $("#"+id).val() != 'undefined'){
				var boxLen = main.field_counting[id];
				var defLen = $("#"+id).val().length;
				if(defLen<=boxLen){	
					$('#'+id+'_len').html((boxLen-defLen));
				}else{
					$("#"+id).val($("#"+id).val().substr(0,boxLen));
					$('#'+id+'_len').html('0');
				}
			}
		},500);

	},
	
	addHashValue:function(text){
	
		document.location.hash = text;
	
	},
	
	search_opt:function(){
		if($('#search_opt').css('display')=='none'){
			$('#search_opt').show();
		}else{
			$('#search_opt').hide();
		}	
	},
	
	url_reload:function(c){
	
		var url = document.location.href;
		var hash = document.location.hash;
			hash = hash.substr(1);
		if(hash.length!=0){
			if(hash.substr(0,main.domain.length) == main.domain){hash = hash.substr(main.domain.length);}
			if(hash.substr(0,1)!='/'){hash = '/'+hash;}
			url = main.domain+hash;
			c = 1;
		}
		if(c==1){
			window.open(url,'_self');
		}
	},
	
	pgChange:function(id){
		$("#"+id+" table.pg_navi a").click(function(){
			var href = $(this).attr('href');		
				
			var pos = href.search(/\?/);
			var len = href.length;
			var test = href.substr(pos-1,1);
			var newHref = href.substr(0,pos);
			if(test != '/'){newHref = newHref + '/';}
			main.addHashValue(href);
			newHref = newHref + 'page/' + href.substr(pos);

			//Scroll to TOP
			
			var fTop = $("#"+id).offset().top;
			var mTop = $(document).scrollTop();
			if(fTop<mTop){
				var selfID = setInterval(function(){
					mTop -= 100;
					if(fTop>=mTop){mTop = fTop;}
					$(document).scrollTop(mTop);
					if(fTop>=mTop){
						clearInterval(selfID);
						main.shadow(id);
						$("#"+id).load(newHref,function(){main.shadow(id,1);main.toolz('#'+id+' a');});	
					}
				},10);				
			}else{
				main.shadow(id);
				$("#"+id).load(newHref,function(){main.shadow(id,1);main.toolz('#'+id+' a');});	
			}
			
			return false;
		});	
	},
	
	btnValue:function(id,val){
		if(typeof lang[val] != 'undefined'){
			val = lang[val];
			$('#'+id).val(val);
		}			
		return true;	
	},
	
	
	voteTable:function(id,n,url){
	
		$('#'+id+' a').bind('mouseover',function(){			
			$('#'+id).css({'background-position':(-200 + parseInt($(this).attr('rel'))*20)+'px top'});		
		}).bind('mouseout',function(){			
			$('#'+id).css({'background-position':(-200 + parseInt(n)*20)+'px top'});		
		}).bind('click',function(){			
			var vote = $(this).attr('rel');			
			
				$.get(main.domain+url,{'vote':vote,'js':1},function(data){$('#R'+id).html(data);$('#'+id+' a').remove();});
			
			return false;
		});
	
	},
	
	coment_area:function(txt,c){
		$('#_coment').bind('focus',function(){if($(this).val() == txt){$(this).val('').removeClass('pre');$('#_coment_name').show();$('#_coment_btn').show();main.startCount('','_coment');}});
		if(parseInt(c)!=0){
			main.replay_select = c;
			$('#_coment').focus();
		}		
	},
	
	add_coment:function(urls,id){
		var txt = $('#_coment').val();
		var coment_rnd = $('#coment_rnd').val();
		var coment_re = $('#coment_re').val();
		if(txt.length>0 && typeof urls != 'undefined'){
			main.replay_select = 0;
			main.shadow(id);
			$.post(main.domain+urls,{'add_coment':1,'coment_txt':txt,'coment_re':coment_re,'coment_rnd':coment_rnd},function(data){$('#'+id).html(data);main.toolz('#'+id+' a');main.shadow(id,1);});
		}	
		return false;
	},
	coment_area_txt:{},
	replay_select:0,
	coment_replay:function(cid){
	
		if(main.replay_select!=0 || main.replay_select==cid){
			$('#replay_a_'+main.replay_select).removeClass('c').addClass('r').html(main.coment_area_txt[2]);$('#hoverDiv_'+main.replay_select).addClass('hoverDiv');
		}else if(main.replay_select==0){
			$('#last_coment').hide();
		}
		
		var coment_box = $('#coment_box').html();
		$('#coment_box').remove();
		
		if(main.replay_select==cid){
			main.replay_select = 0;
			$('#coment_re').val(0);
			$('#last_coment').show().html($('<div/>').addClass('coment_box').attr({'id':'coment_box'}).html(coment_box));
		}else{	

			main.replay_select = cid;
			$('#coment_re').val(cid);
			$('#replay_a_'+cid).removeClass('r').addClass('c').html(main.coment_area_txt[1]);$('#hoverDiv_'+cid).removeClass('hoverDiv');
			$('#coment_replay_'+cid).after($('<div/>').addClass('replay_box').attr({'id':'coment_box'}).html(coment_box));

		}
		
		$('#_coment').val('');
		main.coment_area('',0);
		$('#_coment').focus();		
		main.field_len('_coment','1000',true);
			
		return false;
	},
	
	
	favTxt:'',
	favStatus:function(pid,t1,t2,url){
		if($('#pidFavorite').html()==t2){
			main.favTxt = t1;
			$('#pidFavorite').removeClass('favon').addClass('favoff');
		}else{
			main.favTxt = t2;
			$('#pidFavorite').removeClass('favoff').addClass('favon');			
		}			

		$('#pidFavorite').html(main.favTxt);
		
		$.get(main.domain+'/'+url+'/page/',{'pid':pid,'favorite':1,'js':1});		
		
		return false;
	},
	
	
	fight_vote:function(url,p){
		
		$('#f_voting').append(
			$('<div/>').addClass('mag_nr').width($('#f_voting').width()).height($('#f_voting').height()).html(
				$('<div/>').attr('id','f_votings').html('+'+p)
			)
		);
		$('#f_voting table').hide();
		$('#f_votings').animate({'top':'-15px','left':'300px','font-size':'40px','opacity':0},1500,function(){
			$('#f_voting').remove();
		});		
		$.post(main.domain+url,{'_voting':1});	
	
		return false;
	}
}    
