function imagesPaging(){
	var parts = this.hash.split(';');
	var user = parts[0].substr(1,parts[0].length-1);
	var page = parts[1];
	var el = $('div.mygallery');
	el.html('<div style="width:100%;height:100%" class="loading"></div>');
	//el.addClass('loading');
	el.load('/teenid/ajax.php?act=images&offset='+page+'&user='+user,function(){
		//el.removeClass('loading');
		$('.mygallery a.ajaxpaging').click(imagesPaging);
	});

}

function rotateNews(){
	//alert(nli);
	//alert($('div.featured ul').children('li'));
	//alert($('div.featured ul').children('li')[nli].nodeName);
	$('ul#fimage li:visible').fadeOut();
	$($('ul#fimage').children('li')[nli]).fadeIn('slow',function(){
		$('div.fimagebgbott a').removeClass('on');
		$($('div.fimagebgbott ul li.article').children('a')[nli]).addClass('on');
		nli++;
		var nlis = nli + '';
		if(nlis.length<2)nlis='0'+nlis;
		//$('div.fimagebgbott .cwd').html(nlis);
		if (nli==$('ul#fimage').children('li').length) nli=0;
	});
}

var nli=1;

$(document).ready(function(){
	setTimeout(function(){
		
		if(document.getElementById('fimage')){
			var rn = setInterval(rotateNews,500000);
			$('ul#fimage').css({position:'relative',height:'370px'});
			$('ul#fimage li').css({position:'absolute',width:'100%',height:'100%',left:'0px',top:'0px'});
		}
		
		$('div.fimagebgbott li.article a').click(function(){
			clearInterval(rn);
			nli = $('ul#fimage li').index($(this.hash));
			rotateNews();
			return false;
		});

		$('div.fimagebgbott .next').click(function(){
			clearInterval(rn);
			//nli++;
			//if (nli==$('div.featured ul').children('li').length) nli=0;
			rotateNews();
			return false;
		});

		$('div.fimagebgbott .back').click(function(){
			clearInterval(rn);
			if (nli==0){
				nli=$('ul#fimage').children('li').length-2;
			}
			else{
				nli=nli-2;
			}
			if (nli<0)nli=$('ul#fimage').children('li').length-1;
			rotateNews();
			return false;
		});
	},50);
	
	$('.mygallery a.ajaxpaging').click(imagesPaging);
	
	$('a.lightbox').click(lightbox_show);
	
	$('div.rate form').submit(function(){
		var url = this.action;
		var r = $('div.rate form input:checked').val();
		$.post(url,{rate:r},function(response){
			$('#rateBar').css({width:Math.round(response/6*100)+'%'});
			$('#rating').html(response);
			$('div.rate form').fadeOut();
			$('#rateBox').fadeIn();
		}); 
		return false;
	});
	
	$('#favimage').click(function(){
		$(this).addClass('loading');
		$.get(this.href,function(response){
			$('#favimage').remove();
			lightbox_message(response);
			/*
			$('#favimage').removeClass('loading');
			$('#favimage').addClass('favorite');
			$('#favimage').html('');
			*/
		});
		return false;
	});
	
	$('#imageVote input').click(function(){
		$('#imageVote').submit();
	});
	
	$('a.delete').click(function(){
		if(!confirm('Наистина ли искате да изтрите това?')){
			return false;
		}
	});

	$('a.star').click(function(){
		$.get(this.href,function(response){
			lightbox_message(response);
		});
		return false;
	});
	
	$('a.friend').click(function(){
		$.get(this.href,function(response){
			lightbox_message(response);
		});
		return false;
	});
	
	$('a.print').click(function(){
		window.open(this.href,'','scrollbars=yes,resizable=no,width=680,height=480');
		return false;
	});
	
	$('#CheckboxAll').click(function(){
		$("input[type='checkbox']").attr('checked', $('#CheckboxAll').is(':checked'));   
   	});
   	
	$('#CheckboxAll1').click(function(){
		$("input[type='checkbox']").attr('checked', $('#CheckboxAll1').is(':checked'));   
   	});
	 
	
});

function lightbox_show(){

	if(document.getElementById('TB_overlay')){
		$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
	}

	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		$("body","html").css({height: "100%", width: "100%"});
		$("html").css("overflow","hidden");
		if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div><div id='TB_loader'></div>");
			//$("#TB_overlay").click(lightbox_remove);
		}
	}else{//all others
		if(document.getElementById("TB_overlay") === null){
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div><div id='TB_loader'></div>");
			//$("#TB_overlay").click(lightbox_remove);
		}
	}

	if(tb_detectMacXFF()){
		$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
	}else{
		$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
	}

	$('object, embed').hide();
	
	$("#TB_window").append("<a id='TB_closeWindow' href=''></a><div id='TB_ajaxContent'></div><div id='TB_ajaxFooter'></div>");
	
	$('#TB_ajaxContent').load(this.href,function(){
		$('#TB_closeWindow').click(lightbox_remove);
		$("#TB_window").css({marginTop: '-' + ($("#TB_window").innerHeight()/2) + 'px' });
		$("#TB_window").fadeIn(function(){
			$("#TB_loader").remove();
		});
	});

	return false;
}

function lightbox_message(message){

	if(document.getElementById('TB_overlay')){
		$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
	}

	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		$("body","html").css({height: "100%", width: "100%"});
		$("html").css("overflow","hidden");
		if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
			//$("#TB_overlay").click(lightbox_remove);
		}
	}else{//all others
		if(document.getElementById("TB_overlay") === null){
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
			//$("#TB_overlay").click(lightbox_remove);
		}
	}
	
	if(tb_detectMacXFF()){
		$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
	}else{
		$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
	}

	$('object, embed').hide();
	
	$("#TB_window").append("<a id='TB_closeWindow' href=''></a><div id='TB_ajaxContent'></div><div id='TB_ajaxFooter'></div>");
	
	$('#TB_ajaxContent').html('<div class="lightboxForm" style="padding-bottom:20px;"><p>'+message+'</p></div>');
	$('#TB_closeWindow').click(lightbox_remove);
	$("#TB_window").css({marginTop: '-' + ($("#TB_window").innerHeight()/2) + 'px' });
	$("#TB_window").fadeIn();

	return false;
}

function lightbox_remove() {
	$("#TB_window, #TB_overlay").fadeOut("fast",function(){
		$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
		$('object, embed').show();
	});
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function lightbox_submit(form){
	var data = new Object();
	data['ajax']=1;
	for(i=0;i<form.elements.length;i++){
		var el=form.elements[i];
		data[el.name]=el.value;
	}
	$.post(form.action,data,function(response){
		lightbox_message(response);
	});
	return false;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function imagePreview(url){
	var id = Math.floor(Math.random() * 99999);
	$("#imagePreview").html($("#imagePreview").html() + "<div class='img' id='" + id + "'><a href='#" + id + "' class='button delete'></a><img src='" + url + "' width='60' alt='' /><input type='hidden' name='images[]' value='" + url + "' /></div>");
	$("#imagePreview a.delete").click(function(){
		$(this.hash).fadeOut();
	});
}

function teenbrowser(field_name, url, type, win) {

    var cmsURL = '/teenid/browser.php';
    tinyMCE.activeEditor.windowManager.open({
	        file : cmsURL + "?type=" + type,
	        title : "File Browser",
	        width : 600,
	        height : 400,
	        close_previous : "no"
	    }, {
	        window : win,
	        input : field_name,
	        resizable : "yes",
	        inline : "yes",
	        editor_id : tinyMCE.selectedInstance.editorId
	    }
	);

    return false;
}

function URLConverter(url, node, on_save) {
	return url;
}


function Bookmark(url, title) {
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.external ) { 
		window.external.AddFavorite( url, title); 
	}
	else if(window.opera && window.print) {
		return true; 
	}
	
 }

function clearform(which,text){
	var $txt = text
	if (which.value==$txt) {
		which.value='';
	} else if (which.value=="") {
		which.value=$txt;
	}

}
