var outMenu = false;
var outLI = false;

var menuOpen = false;
var menuPositionX = 0;
var menuHeight = 0;
var mainBoxy = false;

var changeToUrl = '';

KeyCodes = function () {};

KeyCodes.ESCAPE = 27;
KeyCodes.ENTER  = 13;
KeyCodes.UP = 38;
KeyCodes.DOWN = 40;

function hideSubmenus()
{
	if (outMenu == true && outLI == true) $('ul.submenu').css('left','-9999px');
}
function showLogin()
{
	loadPostAjaxBox('login/ajax',{url:document.location.href},false);
	return false;
}
function showDark()
{
	$('#dark').show();
	$('#dark').click(function(){
		$('#dark').hide();
		if (mainBoxy) mainBoxy.hide();
		mainBoxy = false;
		$('.boxy-wrapper').remove();
	});
	
}

var lastSendSearchRequest = 0;
var lastSearchString = '';
var newSearch = '';
var searchTarget = '';
var searchChannel = '';
function checkForSearching()
{
	if (newSearch != lastSearchString)
	{
		var time = new Date();
		var cTime = time.getTime();
		if (lastSendSearchRequest < (cTime-1000))
		{
			lastSearchString = newSearch;
			$.post(BASE + 'editor/channel/'+searchChannel+'/ajax',{q:lastSearchString}, function(e){
				$('#'+searchTarget).html(e.content);
				$.scrollTo($('.boxSearch'));
			},'json');
			
			lastSendSearchRequest = cTime;
		}
	}
}
var startedCron = false;
function searchCron()
{
	checkForSearching();
	setTimeout('searchCron()',100);
	
}
function searchEditor(value, target, channel)
{
	
	if (value.substring(0,5)=="http:") return false;
	searchTarget = target;
	searchChannel = channel;
	newSearch = value;
	if (startedCron == false) searchCron();
	startedCron = true;
	return false;
}
function deleteGB(id)
{
	$.post(BASE + 'guestbook/delete/'+id+'/ajax',[], function(e){
		$('#gb'+id).slideUp('slow');
		});
}
function deleteComment(id)
{
	$.post(BASE + 'comments/delete/'+id+'/ajax',[], function(e){
		$('#comment'+id).slideUp('slow');
		});
}
function showNews()
{
	$('#news').toggle();
	$('#news').html('<img src="'+BASE+'static/images/ajax-loader-small.gif" alt="loading ..." />');
	
	var pos = getPosition($('div.middle').get(0));
	$('#news').css('left',pos.x+641);
	$('#news').css('top',pos.y+34);
	loadIntoDiv('news','requests/newsbox/ajax')
}
var test = false;
function centerPlayIcon(o, t)
{
	var pos = getPosition($('#'+t).get(0));
	
	$('#'+o).css('left',pos.x);
	$('#'+o).css('top',pos.y);
	
	if (pos.x > 0 && pos.y > 0)
	{
		//$('#'+o).show();
	}
}

function showVideo(id, title, searching, channel)
{
	if (!searching) searching = '-';
	if (!channel) channel = '';
	$.post(BASE + 'videos/view/'+id+'/'+searching+'/'+channel+'/ajax',{title:title}, function(e){
		$('.boxy-wrapper').remove();
		if (!mainBoxy) mainBoxy = new Boxy('<p>'+e.content+'</p>',{title:e.title});
		else {
			
			mainBoxy.show();
			mainBoxy.setContent(e.content);
			mainBoxy.setTitle(e.title);
		}
	},'json');
	showDark();
}


function initInputs()
{
	$('input.previewText').click(function(){
		if ($(this).val()==$(this).attr('title')) {
			$(this).val('');
			$(this).css('color','black');
		}
		
	});
	$('input.previewText').focus(function(){
		if ($(this).val()==$(this).attr('title')) {
			$(this).val('');
			$(this).css('color','black');
		}
		
	});
	$('input.previewText').blur(function(){
	
		if ($(this).val()=='') {
			$(this).val($(this).attr('title'));
			$(this).css('color','#777777');
		}
		
	});
	$('input.previewText').each(function(){
		$(this).val($(this).attr('title'));
		$(this).css('color','#777777');
	});
	
	$('input').focus(function(){
		$(this).addClass('active');
	});
	
	$('input').blur(function(){
		$(this).removeClass('active');
	});
}


function feedback()
{
	if (!mainBoxy) {
		$('.boxy-wrapper').remove();
		mainBoxy = new Boxy('<img src="'+BASE+'static/images/ajax-loader.gif" alt="loading ..." />', {
			title: ""
		});
	}
	$.post(BASE + 'contact/ajax',{url:document.location.href}, function(e){
		
		
		mainBoxy.setContent(e.content);
		mainBoxy.setTitle(e.title);
		
		mainBoxy.show();
		mainBoxy.center();
		var options = { 
	        target:        'div.boxy-inner',   
	        beforeSubmit:  showRequest,
	        dataType: 'json',
	        success:       showResponse  
	    }; 
	 
	    $('.boxy-wrapper form').submit(function() { 
	        $(this).ajaxSubmit(options); 
	 		return false; 
	    }); 
		initInputs();
	},'json');
	showDark();
	
}

function loadPostAjaxBox(url, post,inputs)
{
	if (!mainBoxy) {
		$('.boxy-wrapper').remove();
		mainBoxy = new Boxy('<img src="'+BASE+'images/ajax-loading.gif" alt="loading ..." />', {
			title: ""
		});
	}
	$.post(BASE + url,post, function(e){
		
		
		mainBoxy.setContent(e.content);
		mainBoxy.setTitle(e.title);
		
		mainBoxy.show();
		mainBoxy.center();
		var options = { 
	        target:        'div.boxy-inner',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        dataType: 'json',
	        success:       showResponse  // post-submit callback 
	 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    }; 
	 
	    
		if (inputs == true) {
			initInputs();
			// bind to the form's submit event 
			$('.boxy-wrapper form').submit(function(){
				// inside event callbacks 'this' is the DOM element so we first 
				// wrap it in a jQuery object and then invoke ajaxSubmit 
				$(this).ajaxSubmit(options);
				
				// !!! Important !!! 
				// always return false to prevent standard browser submit and page navigation 
				return false;
			});
		}
	},'json');
	showDark();
}

function loadAjaxBox(url, allInBox)
{
	if (!mainBoxy) {
		$('.boxy-wrapper').remove();
		mainBoxy = new Boxy('<img src="'+BASE+'images/ajax-loading.gif" alt="loading ..." />', {
			title: ""
		});
	}
	$.post(BASE + url,[], function(e){
		
		
		mainBoxy.setContent(e.content);
		mainBoxy.setTitle(e.title);
		
		mainBoxy.show();
		mainBoxy.center();
		var options = { 
	        target:        'div.boxy-inner',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        dataType: 'json',
	        success:       showResponse  // post-submit callback 
	 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    }; 
	 
	    // bind to the form's submit event 
	    $('.boxy-wrapper form').submit(function() { 
	        // inside event callbacks 'this' is the DOM element so we first 
	        // wrap it in a jQuery object and then invoke ajaxSubmit 
	        $(this).ajaxSubmit(options); 
	 
	        // !!! Important !!! 
	        // always return false to prevent standard browser submit and page navigation 
	        return false; 
	    }); 
		initInputs();
	},'json');
	showDark();
	
}
function deleteFriendship(friend)
{
	$.post(BASE + 'friends/deleteFriendship/'+friend+'/ajax',[],function(e){
		$('div#'+e.div).hide('slow');
	},'json');
	
	
}
function loadIntoDiv(div, url)
{
	$('#'+div).html('<img src="'+BASE+'static/images/ajax-loader-small.gif" alt="Loading" />');
	$.post(BASE + url,[],function(e){
		$('#'+div).html(e.content);
	},'json');
}
function loadTab(classes, url, tab)
{
	$('.'+classes+' .body .content').html('<img src="'+BASE+'static/images/ajax-loader.gif" alt="Loading" />');
	$.post(BASE + url,[],function(e){
		$(''+classes+' .body .content').html(e.content);
		$(''+classes+' .body .pages').html(e.pages);
		if (tab!=null) $(''+classes+' .active').removeClass('active');
		$(tab).addClass('active');
	},'json');
	return false;
}
function hideTalk(id)
{
	$.post(BASE + 'messages/hide/'+id+'/ajax',[],function(e){
		$('div#talk'+id).hide('slow');
	},'json');
	
	
}
var found = false;
var foundUrl = '';

function checkEditorInput(channel)
{
	
	var input = $('#editorInput').val();
	if (input.substring(0,5)!="http:") {
		return searchEditor(input,'res', channel);
	}
	// http://cdn.clipkit.de/916/playlist/3424/player.js
	// http://www.myvideo.de/watch/7806667/Element_Of_Crime_Im_Himmel_ist_kein_Platz_mehr_fuer_uns_zwei
	if (input.search(/cdn.clipkit./i)>0)
	{
		$('#editorForm').attr('action','javascript:;');
		$('#editorForm').unbind('submit');
		$('#editorForm').submit(function(){
			$.post(BASE + 'channels/setClipkit/ajax',{url:input,c:channel},function(e){
				alert('Wurde gesetzt.');
				var url = document.location.href;
				document.location.href = url+'?new';
			},'json');
			return false;
		});
		
		if (found == false)
		{
			foundUrl = input;
			found = true;
			$('#loading').show();
			$.post(BASE + 'videos/getInfo/ajax',{url:input},function(e){
				//$('#videoImgImg').attr('src', e.thumbnail);
				$('#videoInfo').show();
				$('#loading').hide();
				$('#videoDescription').html('<span style="font-size:15px;">'+e.title+'</span><br />'+e.description);
				if (e.show == 0)
				{
					$('#previewButton').hide();
				}
				else {
					$('#previewButton').show();
					$('#previewButton').val('Setzen');
				}
			},'json');
		}
		return;
	}
	if (input.search(/vimeo.+/))
	{
		
		$('#editorForm').attr('action','javascript:;');
		$('#editorForm').unbind('submit');
		$('#editorForm').submit(function(){
			$.post(BASE + 'videos/showVideo/ajax',{url:input},function(e){
				if (channel == '' || channel == 'undefined') channel = '';
				
				showVideo(e.id, e.title,'-',channel); 
			},'json');
			return false;
		});
		
		if (found == false)
		{
			foundUrl = input;
			found = true;
			$('#loading').show();
			$.post(BASE + 'videos/getInfo/ajax',{url:input},function(e){
				$('#videoImgImg').attr('src', e.thumbnail);
				$('#videoInfo').show();
				$('#loading').hide();
				$('#videoDescription').html('<span style="font-size:15px;">'+e.title+'</span><br />'+e.description);
				if (e.show == 0)
				{
					$('#previewButton').hide();
				}
				else {
					$('#previewButton').show();
				}
			},'json');
		}
		else {
			if (input != foundUrl)
			{
				found = false;
				foundUrl = '';
				$('#loading').hide();
			}
		}
	}
	if (input.search(/youtube.+/))
	{
		
		$('#editorForm').attr('action','javascript:;');
		$('#editorForm').unbind('submit');
		$('#editorForm').submit(function(){
			$.post(BASE + 'videos/showVideo/ajax',{url:input},function(e){
				if (channel == '' || channel == 'undefined') channel = '';
				
				showVideo(e.id, e.title,'-',channel); 
			},'json');
			return false;
		});
		
		if (input.search(/watch.+/)>0 && found == false)
		{
			foundUrl = input;
			found = true;
			$('#loading').show();
			$.post(BASE + 'videos/getInfo/ajax',{url:input},function(e){
				$('#videoImgImg').attr('src', e.thumbnail);
				$('#videoInfo').show();
				$('#loading').hide();
				$('#videoDescription').html('<span style="font-size:15px;">'+e.title+'</span><br />'+e.description);
				if (e.show == 0)
				{
					$('#previewButton').hide();
				}
				else {
					$('#previewButton').show();
				}
			},'json');
		}
		else {
			if (input != foundUrl)
			{
				found = false;
				foundUrl = '';
				$('#loading').hide();
			}
		}
	}
}
function markStars(many, id)
{
	for (var a = 0; a < many; a++)
	{
		$('#image'+id+(a+1)).attr('src',BASE+'static/images/star_big_active.png');
	}
	for (var a = many; a < 5; a++)
	{
		$('#image'+id+(a+1)).attr('src',BASE+'static/images/star_big_inactive.png');
	}
}
function vote(vote, targetId, type, newWidth, id)
{
	$.post(BASE + 'voting/vote/'+targetId+'/'+type+'/'+vote+'/ajax',[],function(e){
		$('#mouse'+id).remove();
		$('#n'+id).show();
		$('#n'+id).unbind('mouseover');
		$('#n'+id).attr('onmouseover','');
		$('#'+id).css('width',newWidth+'px');
		alert('Danke für Deine Bewertung.');
	},'json');
}
function takeFav(type,id)
{
	if (type == 'video') {
		$('#remember' + id).html('merken ..');
	}
	$.post(BASE + 'favorites/take/'+type+'/'+id+'/ajax',[],function(e){
		$('#favo').html('Favorisiert.');
		$('.greenbar').show();
		if (type == 'video')
		{
			$('#remember'+id).html('Gemerkt');
		}
	},'json');
}
function addVideoInChannel(hoster, id, channel,currentChannel)
{
	if (currentChannel == "") currentChannel = "-";
	if (channel=='') return;
	$.post(BASE + 'channels/addvideo/'+hoster+'/'+id+'/'+channel+'/'+currentChannel+'/ajax/alert',[],function(e){
		$('div.videos div.body').append(e.code);
		$('#addVideo').html('<div class="greenbar" style="480px;margin-bottom:10px;"><div class="end"><div style="width:478px;" class="inner">'+e.content+'</div></div></div>');
	},'json');
}
function share()
{
	var urlToShare = document.location.href;
	
	var shareWindow = window.open(BASE + 'share/'+urlToShare+'/popup', "Weiterempfehlen", "width=690,height=610,status=no,scrollbars=no,resizable=no");
	shareWindow.focus();

}
function openGoogleSearch(searching)
{
	var url = 'http://images.google.de/images?hl=de&q='+searching;
	
	var shareWindow = window.open(url, "Google-Bildersuche", "width=1024,height=768,status=yes,scrollbars=yes,resizable=yes");
	shareWindow.focus();

}
function addFriend(link, id)
{
	if (!mainBoxy) {
		$('.boxy-wrapper').remove();
		mainBoxy = new Boxy('<img src="'+BASE+'images/ajax-loading.gif" alt="loading ..." />', {
			title: ""
		});
	}
	var textVal = $('#friendText').val();
	$.post(BASE + 'friends/addSure/'+link+'/'+id+'/ajax',{text:textVal}, function(e){
		
		
		mainBoxy.setContent(e.content);
		mainBoxy.setTitle(e.title);
		
		mainBoxy.show();
		mainBoxy.center();
		var options = { 
	        target:        'div.boxy-inner',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        dataType: 'json',
	        success:       showResponse  // post-submit callback 
	 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    }; 
	 
	    // bind to the form's submit event 
	    $('.boxy-wrapper form').submit(function() { 
	        // inside event callbacks 'this' is the DOM element so we first 
	        // wrap it in a jQuery object and then invoke ajaxSubmit 
	        $(this).ajaxSubmit(options); 
	 
	        // !!! Important !!! 
	        // always return false to prevent standard browser submit and page navigation 
	        return false; 
	    }); 
		initInputs();
	},'json');
	showDark();
	
}
function comment(type, id, textId, target)
{
	$('#ctloading').show();
	var textVal = $('#'+textId).val();
	urlToChange = '';
	$.post(BASE + 'comments/comment/'+type+'/ajax',{text:textVal,object:id},function(e){
		
		var old = $('#'+target).html();
		$('#'+target+' div.guestbookEntry').remove();
		$('#'+target).append(e.content + old);
		$('#comment'+e.id).slideDown('slow');
		$('#'+textId).val('');
		$('#ctloading').hide();
		if (urlToChange != '')
		{
			document.location.href = urlToChange;
		}
	},'json');
}
function guestbook(id, textId, target)
{
	$('#gbloading').show();
	var textVal = $('#'+textId).val();
	
	$.post(BASE + 'guestbook/comment/ajax',{text:textVal,object:id},function(e){
		
		var old = $('#'+target).html();
		$('#'+target+' div.guestbookEntry').remove();
		$('#'+target).append(e.content + old);
		$('#gb'+e.id).slideDown('slow');
		$('#'+textId).val('');
		$('#gbloading').hide();
	},'json');
}
function addVideoInPlayer(hoster, id, target)
{
	
	var url = 'channels/addvideo/'+hoster+'/'+id+'/0/ajax';
	$.post(BASE + url,[], function(e){
		
		$('#'+target).html(e.content);
		
		$('#'+target+' a').click(function(){
			target_ = $(this).attr('href') + '/ajax';
			$.post(target_,[],function(e){
				
				$('#'+target).html(e.content);
				if (mainBoxy) mainBoxy.center();
			},'json');
			return false;
		});
		
		if (!mainBoxy) mainBoxy.center();
		var options = { 
	        target:        'div.boxy-inner',   // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        dataType: 'json',
	        success:       showResponse  // post-submit callback 
	 
	        
	    }; 
	 
	    // bind to the form's submit event 
	    $('.boxy-wrapper form').submit(function() { 
	        $(this).ajaxSubmit(options); 
	 		return false; 
	    }); 
		
		
		
	},'json');
	
}

function closeBoxy()
{
	mainBoxy.hide();
	mainBoxy = false;
	$('#dark').hide();
	$('.boxy-wrapper').remove();
	$('#myytplayer').remove();
}

/*function checkSubmenus()
{
	if (outMenu == true && outLI == true)
	{
		setTimeout("hideSubmenus()",1000);
		
	}
}*/


// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
 	if (mainBoxy)
	{
		mainBoxy.setContent('<img src="'+BASE+'images/ajax-loading.gif" alt="loading ..." />');
		mainBoxy.center();
	}
    return true; 
} 
 
function checkLoginForm(e)
{
	var f = 0;
	if (e)
	{
		f = e.which;
	}
	else {
		f = window.event.keyCode;
	}
	if (f == 13)
	{
		$('#loginForm').submit();
	}
}
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
	var options = {
		target: 'div.boxy-inner', // target element(s) to be updated with server response 
		beforeSubmit: showRequest, // pre-submit callback 
		dataType: 'json',
		success: showResponse // post-submit callback 
	};
	
	$('.boxy-inner').html(responseText.content);
	mainBoxy.center();
	
	$('.boxy-wrapper form').submit(function() { 
	    $(this).ajaxSubmit(options); 
	 	return false; 
	}); 
	initInputs();
} 

function getPosition(element)
    {
        var elem=element,tagname="",x=0,y=0;
        while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined"))
        {
            y+=elem.offsetTop;
            x+=elem.offsetLeft;
            tagname=elem.tagName.toUpperCase();

            if (tagname=="BODY")
                elem=0;
                if (typeof(elem)=="object")
                if (typeof(elem.offsetParent)=="object")
                    elem=elem.offsetParent;
        }
        position=new Object();
        position.x=x;
   position.y=y;
   return position;
}

var autoFillExists = false;
var autoFillValues = new Array();
var autoFillOpen = false;
var autoFillCurrentValue = 0;
var autoFillTarget = null;


function handleKey(key) {

   
    // handle key is just for the search box @ the moment
    if (autoFillOpen == false) return;
	
    var click = false;
    switch (key.keyCode)
    {
        case KeyCodes.UP:
            if (autoFillOpen == true) {
                autoFillCurrentValue --;
                if (autoFillCurrentValue<0) autoFillCurrentValue = autoFillValues.length-1;
                showAutoFill(autoFillTarget);
            }
            break;
        case KeyCodes.DOWN:
			if (autoFillOpen == true) {
				
                autoFillCurrentValue ++;
                if (autoFillCurrentValue>=autoFillValues.length) autoFillCurrentValue = 0;
                
				showAutoFill(autoFillTarget);
            }
            
            break;
        case KeyCodes.ENTER:
            
            if (autoFillOpen == true) {
                $('div#autofill div.entry:eq('+autoFillCurrentValue+')').click();
                
                click = true;
            }
            break;
    }
    /*if (rke.searchBox != null) {
        if (rke.searchBox.contentbox && rke.searchBox.visible) textfieldEnd(rke.searchBox.contentbox);
    }
    if (rke.selectBoxOpen == true || click == true) {
        textfieldEnd($('#selectBox div.entryactive').get(0));
    }*/
}


function showAutoFill(target)
{
	
	if (autoFillExists == false)
	{
		var t = jQuery('<div id="autofill"></div>');
		$('body').append(t);
		
		
		
		autoFillTarget = target;
		
	}
	var pos = getPosition(target);
	$('#autofill').css('left',pos.x+"px");
	$('#autofill').css('top',(pos.y+4+$(target).height())+"px");
	$('#autofill').html('');
	var query = $(target).val();
	
	for (var a = 0; a < autoFillValues.length; a++)
	{
		var fillValue = autoFillValues[a].show;
		var pos = autoFillValues[a].show.toLowerCase().search(query);
		var formatedTitle = autoFillValues[a].show;
		formatedTitle = formatedTitle.substring(0,pos)+"<span style=\"font-weight:bold;\">"+formatedTitle.substring(pos,pos+query.length+1)+"</span>"+formatedTitle.substring(pos+query.length+1);
		fillValue = formatedTitle;
		var c = jQuery('<div onmouseover="$(this).addClass(\'over\');autoFillCurrentValue='+a+';" onmouseout="$(this).removeClass(\'over\');" class="entry'+(autoFillCurrentValue==a?' over':'')+'" onclick="$(\'#'+$(target).attr('id')+'_val\').val('+autoFillValues[a].value+');$(\'#'+$(target).attr('id')+'\').val(\''+autoFillValues[a].show+'\');$(\'#autofill\').hide();autoFillOpen=false;autoFillCurrentValue=0;oldValue=\''+autoFillValues[a].value+'\';">'+fillValue+'</div>');
		$('#autofill').append(c);
	}
	if (autoFillValues.length > 0)
	{
		$('#autofill').show();
		autoFillOpen = true;
	}
	else {
		$('#autofill').hide();
		autoFillOpen = false;
	}
}
var oldValue = '';
var titleOpen = false;
var titleX = 0;
var titleY = 0;

function autoFillTown(target, currentValue)
{
	
	if (currentValue.length > 2 && currentValue != oldValue)
	{
		$.post(BASE + 'ajax/townsuggestion/'+currentValue,[],function(e){
		autoFillValues = e.towns;
		showAutoFill(target);
		autoFillTarget = target;
		oldValue = currentValue;
	},'json');
	}
	showAutoFill(target);
}

function changeDays(b)
{
	var month = $(b).val();
	var numberOfDays = 31;
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) numberOfDays = 31;
	if (month == 4 || month == 6 || month == 9 || month == 11) numberOfDays = 30;
	if (month == 2) numberOfDays = 29;
	
	var content = '<option value="">Tag</option>';
	
	var selectedIndex = $(b).parent().find('select.days').val();
	
	
	for (var a = 1; a < numberOfDays+1; a++)
	{
		content += '<option value="'+a+'"'+(selectedIndex==a?' selected="selected"':'')+'>'+a+'.</option>';
	}
	
	$(b).parent().find('select.days').html(content);
}
function reloadChannelPage()
{
	document.location.href = BASE + 'channels/search/category/'+$('#cat').val()+'/'+$('#orderby').val();
}
function addVideo(hoster, id)
{
	if (!mainBoxy) {
		$('.boxy-wrapper').remove();
		mainBoxy = new Boxy('<img src="'+BASE+'images/ajax-loading.gif" alt="loading ..." />', {
			title: ""
		});
	}
	var url = 'channels/addvideo/'+hoster+'/'+id+'/0/ajax';
	
	$.post(BASE + url,[], function(e){
		
		
		mainBoxy.setContent(e.content);
		mainBoxy.setTitle(e.title);
		
		mainBoxy.show();
		mainBoxy.center();
	
	 
		$('.boxy-wrapper a').click(function(){
			var target_ = $(this).attr('href') + '/ajax';
			$.post(target_,[],function(e){
				
				mainBoxy.setContent(e.content);
				mainBoxy.show();
				mainBoxy.center();
			},'json');
			return false;
		});
		
		initInputs();
	},'json');
	showDark();
	
	
	
}

$(document).ready(function(){
	$('div.bar ul.topMenu li').mouseover(function(){
		var where = jQuery.iUtil.getPosition(this).x;
		$('ul.submenu').css('left','-9999px');
		$('ul',this).css('left',where+'px');
		menuOpen = true;
		menuPositionX = where;
		menuHeight = $('ul',this).height();
		
	});
	$('body').mousemove(function(e) {
		if (menuOpen == true)
		{
			var x = e.pageX;
			var y = e.pageY;
			if (x < menuPositionX || x > (menuPositionX+160) || y < 143 || y > (163+menuHeight))
			{
				$('ul.submenu').css('left','-9999px');
				menuOpen = false;
			}
		}
		if (titleOpen == true)
		{
			var x = e.pageX;
			var y = e.pageY;
			if (x > titleX+100 || x < (titleX-20) || y < titleY || y > (163+titleY))
			{
				$('div#title').hide();
				titleOpen = false;
			}
		}
		
   	});
	$("#group_search").keyup(function(e)
	{
		if(this.value.length>3)
		{
			$(".ajax_content").css('margin','0px');			
			$(".ajax_content").load(BASE + 'groups/search/'+this.value+"/0/ajax/");			
		}
	});
	$('input.town').focus(function(){
		
	});
	$('input.town').keyup(function(){
		var cV = $(this).val();
		autoFillTown(this,cV);
	});

	
	$('div.bar li.item').mouseover(function() {
		$('img.hover',this).show();
		$('img.normal',this).hide();
	});
	$('div.bar li.item').mouseout(function() {
		$('img.hover',this).hide();
		$('img.normal',this).show();
	});
	$('[title!=""]').mouseover(function(){
		if ($(this).hasClass('showinbg') || $(this).hasClass('videoPreview') || $(this).parent().hasClass('input'))
		{
			return;
		}
		var pos = jQuery.iUtil.getPosition(this);
		var x = (pos.x+5);
		var y = (pos.y-25);
		titleX = x;
		titleY = y;
		var title = $(this).attr('title');
		$('div#title').html(title);
		$('div#title').show();
		$('div#title').css('left',(x-105)+'px');
		$('div#title').css('top',y+'px');
		titleOpen = true;
	});
	
	
	$('body').keyup(function(e){handleKey(e);});
	
	$('img.selectable').mouseover(function(){
		$('img.selected').removeClass('selected');
		$(this).addClass('selected');
	});
	$('img.selectable').mouseover(function(){
		//$(this).removeClass('selected');
	});
	
	$('.showinbg').focus(function(){
        var value = jQuery(this).val();
        var title = jQuery(this).attr("title");
		
		$(this).css('color','black');

        if (title == value) {
            jQuery(this).val('');
            jQuery(this).removeClass('hastitle');
			
        }
    });
    $('.showinbg').blur(function(){
        var value = jQuery(this).val();
        var title = jQuery(this).attr("title");
		
		
		
        if (title != value && value=="") {
            jQuery(this).val(title);
            jQuery(this).addClass('hastitle');
			$(this).css('color','#9bb2ce');
		
        }
    });
    $('.showinbg').each(function(){$(this).blur();$(this).css('color','#9bb2ce');});
    
	$('.channelLink a').mouseover(function(){$(this).parent().css('background-color','#8f8f8d')});
	$('.channelLink a').mouseout(function(){$(this).parent().css('background-color','#737372')});
	$('.channelLink a:first').click(function(){if ($(this).parent().hasClass('channelLinkactive')) $(this).parent().removeClass('channelLinkactive'); else $(this).parent().addClass('channelLinkactive');});
	
	$('div.boxChanneledit div.name input').keyup(function(){
		var i = $(this).val();
		if (i.length>35) {
			$(this).val(i.substring(0,35));
		}
	});
	$('div.boxChanneledit div.name input').keydown(function(){
		var i = $(this).val();
		if (i.length>35) {
			$(this).val(i.substring(0,35));
		}
	});
	
	initInputs();
	
	 
});

