/******************************************************/
/*Main javascript object for the site
/******************************************************/
var kanalen = new Object();
kanalen.language=new Array();
kanalen.video=new Object();

function rating_mouseover_wrapper(id)
{
	id = parseInt(id);
	
	for( var i=1; i<=5; i++ )
	{
		var star = _('rating_star'+i);
		star.src = ( id >= i ? 'gfx/filled_star.jpg' : 'gfx/empty_star.jpg' );
	}
}

function rating_mouseout_wrapper()
{
	var scoreobj = _('rating_score');
	var score = (( scoreobj && scoreobj.value ) ? scoreobj.value : 0 ); 
	
	for( var i=1; i<=5; i++ )
	{
		var star = _('rating_star'+i);
		star.src = ( score >= i ? 'gfx/filled_star.jpg' : 'gfx/empty_star.jpg' );
	}
}

function rating_onclick_wrapper(videoid, score)
{
	do_ajax( _('rating'), 'ajax.php?action=ratevideo&videoid=' + videoid + '&score=' + score);
	
	return false;
}


/*
kanalen.video.empty_stats=function()
{
	removeChildren(_("videoStats"));

	for (var i = 1; i <= 5; i++) {
		var star = document.createElement("img");
		star.src = "gfx/empty_star.jpg";
		_("videoStats").appendChild(star);
	}
	var grayedTxt = document.createElement("span");
	grayedTxt.className = "grayedText";
	grayedTxt.innerHTML = " " + kanalen.language['vote_on_video'] + " (0) | " + kanalen.language['comment_video'] + " (0)";
	_("videoStats").appendChild(grayedTxt);
}
*/

kanalen.video.open=function(channelId, videoId) 
{
	
	//removeChildren(_("videoDiv"));
	
	//loadVideo(videoId, channelId, 'videoDiv', 512, 288);
	do_ajax( _('comments'), 'ajax.php?action=fetch_videoComments&videoid=' + videoId);
	//do_ajax( _('videoStats'), 'ajax.php?action=fetch_videoStats&videoid=' + videoId);
	do_ajax( _('rating'), 'ajax.php?action=videorating&videoid=' + videoId);
	this.current_video=videoId;
	this.current_channel=channelId;
	_('comment_box').style.display='none';
	_('embed_box').style.display='none';
	return false;
}

/*
kanalen.video.edit_description=function(video_id)
{
	Sdialog.open('ajax.php?action=edit_video_settings&video_id=' + video_id);
}
*/
/*
kanalen.video.save_settings=function(user_id,video_id,form_el)
{
	var description=make_ajax_clean(form_el.description.value.toString());
	var name=make_ajax_clean(form_el.name.value.toString());
	var tags=make_ajax_clean(form_el.tags.value.toString());
	var video_private=0;
	if(form_el.video_private.checked)
	{
		video_private=1;
	}
	Sdialog.close();

	do_ajax( _('videoList'), 'ajax.php?action=fetch_videoList&uid=' + user_id + '&edit_settings=' + video_id + '&description=' + description + '&name=' + name + '&private=' + video_private + '&tags=' + tags);
}
*/
kanalen.video.comment_box=function() 
{
	if(_('comment_box').style.display=='block')
	{
		_('comment_box').style.display='none';
	}
	else
	{
		_('comment_box').style.display='block';
	}
}

kanalen.video.embed_box=function() 
{
	if(_('embed_box').style.display=='block')
	{
		_('embed_box').style.display='none';
	}
	else
	{
		_('embed_box').style.display='block';
	}
}


kanalen.video.post_comment=function(form_element) 
{
	var name=make_ajax_clean(form_element.comment_name.value);
	var text=make_ajax_clean(form_element.comment_text.value);
	do_ajax( _('comments'), 'ajax.php?action=fetch_videoComments&videoid=' + this.current_video + '&name=' + name + '&comment=' + text );
	form_element.comment_name.value="";
	form_element.comment_text.value="";
	return false;
}

kanalen.video.delete_comment=function(video_id,comment_id)
{
	if (confirm(kanalen.language['profile_confirm_comment_will_be_deleted']))
	{
		make_ajax_request({url:'ajax.php?action=delete_comment',
		data:{commentid:comment_id,videoid:video_id},
		callback:function()
		{
			do_ajax( _('comments'), 'ajax.php?action=fetch_videoComments&videoid=' + video_id + '&' + Math.random() + '=' + Math.random());
		}});
	}
	
	return false;
}
/*
kanalen.video.embedcode_box=function(video_id)
{
	Sdialog.open('ajax.php?action=get_embed_dialog&video_id=' + video_id);
}
*/

/*
kanalen.video.vote = function(videoId) 
{
	for (var i = 1; i <= 5; i++) {
		_("star" + i).src = "gfx/empty_star.jpg";
		_("star" + i).setAttribute("score",i);
		_("star" + i).className = "voteHover";
		_("star" + i).onmouseover = function() {
			for (var j = 1; j <= 5; j++) {
				if (this.getAttribute("score") >= j)
					_("star" + j).src = "gfx/filled_star.jpg";
				else
					_("star" + j).src = "gfx/empty_star.jpg";
			}
		}					
		_("star" + i).onmouseout = function() {
			for (var j = 1; j <= 5; j++) {
				_("star" + j).src = "gfx/empty_star.jpg";
			}
		}					
		_("star" + i).onclick = function() {
			this.onmouseout = ""; //Google chrome and mayby others (not IE or FF) fire onmousout-event when the containerDiv is reloaded by ajax. Whitout this line the stars would be empty after voting.
			do_ajax( _('videoStats'), 'ajax.php?action=fetch_videoStats&videoid=' + videoId + '&vote=' + this.getAttribute("score"));
		}
	}
}
*/

kanalen.video.change_embedcode = function(userid, videoid)
{
	userid = ( userid ? userid : 0 );
	videoid = ( videoid ? videoid : 0 );
	
	var inputobj = _('embed_width');
	if( inputobj )
	{
		var width = parseInt(inputobj.value);
		var height = Math.round(width / 1.7777778);
		this.update_embedcode(userid, videoid, width, height);
	}	
}

kanalen.video.update_embedcode = function(userid, videoid, embedwidth, embedheight)
{
	var textobj = _('embed_text');
	embedwidth = ( embedwidth ? embedwidth : 512 );
	embedheight = ( embedheight ? embedheight : Math.round(embedwidth / 1.7777778) ) + 27;
	userid = ( userid ? userid : 0 );
	videoid = ( videoid ? videoid : 0 );
	
	var inputobj = _('embed_width');
	if( inputobj )
	{
		inputobj.value = embedwidth;
	}
	
	if( textobj )
	{
		textobj.innerHTML = "&lt;object width=\"" + embedwidth + "\" height=\"" + embedheight + "\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"&gt;\n" +
		"&lt;param name=\"allowfullscreen\" value=\"true\"/&gt;\n" +
		"&lt;param name=\"allowscriptaccess\" value=\"always\"/&gt;\n" +
		"&lt;param name=\"movie\" value=\"http://www.kanalen.fi/embed/" + userid + "/" + videoid + ".swf\"/&gt;\n" +
		"&lt;param name=\"wmode\" value=\"transparent\"/&gt;\n" +
		"&lt;embed src=\"http://www.kanalen.fi/embed/" + userid + "/" + videoid + ".swf\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"" + embedwidth + "\" height=\"" + embedheight + "\" wmode=\"transparent\"&gt;&lt;/embed&gt;\n" +
		"&lt;/object&gt;";
	}
}


kanalen.profile=new Object();

kanalen.profile.edit=function(userid)
{
	if(!_('edit_profile_wrapper'))
	{
		alert('error');
		return false;
	}
	do_ajax( _('edit_profile_wrapper'), 'ajax.php?action=edit_profile&uid=' + userid);
	//_('edit_profile_button').onclick=function(){kanalen.profile.cancel_edit(userid);};
}

kanalen.profile.cancel_edit=function(userid)
{
	if(!_('edit_profile_wrapper'))
	{
		return false;
	}
	else
	{
		_('edit_profile_wrapper').innerHTML="";
		//_('edit_profile_button').onclick=function(){kanalen.profile.edit(userid);};
	}
	
	return false;
}

kanalen.profile.save=function(userid,form_element)
{
	
	var name = form_element.profile_name.value.toString();
	var description = form_element.profile_description.value.toString();
	
	make_ajax_request({
	url:'ajax.php?action=save_profile',
	data:
	{
		name:name,
		description:description,
		uid:userid
	}
	});
}

kanalen.profile.change_password=function(userid,form_element)
{
	
	var old_password = form_element.old_password.value.toString();
	var new_password = form_element.new_password.value.toString();
	var new_password_confirm = form_element.new_password_confirm.value.toString();
	
	make_ajax_request({
	url:'ajax.php?action=change_password',
	data:
	{
		user_id:userid,
		old_password:old_password,
		new_password:new_password,
		new_password_confirm:new_password_confirm
	}
	});
}

/*
kanalen.profile.delete_video=function(user_id,video_id)
{
	if (confirm(kanalen.language['profile_confirm_video_will_be_deleted']))
	{
		do_ajax( _('videoList'), 'ajax.php?action=fetch_videoList&uid=' + user_id + '&deleteVideo=' + video_id);
	}
}
*/



kanalen.channels=new Object();

kanalen.channels.select_channel= function(user_id)
{

	
	do_ajax( _('channel'), 'ajax.php?action=fetch_channel&channelId=' + user_id);
	do_ajax( _('videoList'), 'ajax.php?action=fetch_videoList&uid=' + user_id);
	removeChildren(_("comments"));
	//kanalen.video.empty_stats();
	_('comment_box').style.display="none";
	removeChildren(_("videoDiv"));
	
}



kanalen.admin=new Object();
kanalen.admin.current_newsitem=false;
kanalen.admin.current_channel=false;
kanalen.admin.post_newsitem=function(form_element,is_new)
{
	var header=form_element.newsitem_header.value.toString();
	var text=form_element.newsitem_text.value.toString();
	var placement=form_element.newsitem_placement.value.toString();
	var language=form_element.newsitem_language.value.toString();

	if(is_new || !kanalen.admin.current_newsitem)
	{
		var news_id='new';
	}
	else
	{
		var news_id=kanalen.admin.current_newsitem.toString();
	}

	make_ajax_request({url:'ajax.admin.php?do=post_newsitem',
			data:
			{
				header:header,
				text:text,
				placement:placement,
				lang:language,
				id:news_id
			},
			callback:function(response)
			{
				kanalen.admin.get_news_list(1);
				if(news_id=='new')
				{
					kanalen.admin.current_newsitem=response;
					alert('Nyheten tillagd!');
				}
				else
				{
					alert('Nyheten sparad!');
				}
			},post:true});
}

kanalen.admin.get_news_list=function(page)
{
	do_ajax( _('news_list'), 'ajax.admin.php?do=get_news_list&page=' + page );
}

kanalen.admin.delete_newsitem=function(news_id)
{
	news_id=make_ajax_clean(news_id);
	make_ajax_request({url:'ajax.admin.php?do=delete_newsitem&id=' + news_id,
	callback:function()
	{
		kanalen.admin.get_news_list(1);
	}});

	if(news_id==kanalen.admin.current_newsitem)
	{
		kanalen.admin.current_newsitem=false;
	}
}

kanalen.admin.edit_newspost=function(news_id)
{
	news_id=make_ajax_clean(news_id);
	make_ajax_request({url:'ajax.admin.php?do=get_news_header&id=' + news_id,
			callback:function(response)
			{
				_('news_header_text_field').value=response;
			}});

	make_ajax_request({url:'ajax.admin.php?do=get_news_text&id=' + news_id,
			callback:function(response)
			{
				
				_('news_text_field').value=response;
			}});

	make_ajax_request({url:'ajax.admin.php?do=get_news_placement&id=' + news_id,
			callback:function(response)
			{
				_('newsitem_placement').value=response;
			}});
	make_ajax_request({url:'ajax.admin.php?do=get_newsitem_language&id=' + news_id,
			callback:function(response)
			{
				_('newsitem_language').value=response;
			}});
	this.current_newsitem=news_id;
}


kanalen.admin.save_text=function(form_element)
{
	var text=form_element.editortext.value.toString();
	var language=form_element.language.value.toString();
	var section = form_element.section.value.toString();
	
	make_ajax_request({url:'ajax.admin.php?do=save_text&section=' + section,
	data:{text:text,language:language},post:true});
}

kanalen.admin.load_language=function(form_id,language)
{
	var form_el= getElement(form_id);
	if(!form_el)
	{
		return false;
	}
	var section = form_el.section.value.toString();
	make_ajax_request({url:'ajax.admin.php?do=get_text&language=' + language + '&section=' + section,
			callback:function(response)
			{
				form_el.editortext.value = response;
			}});
}


kanalen.admin.save_rules=function(form_element)
{
	var rules=form_element.rules.value.toString();
	var language=form_element.rules_language.value.toString();
	make_ajax_request({url:'ajax.admin.php?do=save_rules',
	data:{rules:rules,language:language},post:true});

}


kanalen.admin.save_frontpage_text=function(form_element)
{
	var header=form_element.frontpage_header.value.toString();
	var text=form_element.frontpage_text.value.toString();
	var language=form_element.frontpage_language.value.toString();
	make_ajax_request({url:'ajax.admin.php?do=save_frontpage_text',
	data:{text:text,header:header,language:language},post:true});

}

kanalen.admin.load_frontpage_language=function(language)
{
	make_ajax_request({url:'ajax.admin.php?do=get_frontpage_header&language=' + language,
			callback:function(response)
			{
				_('frontpage_header').value=response;
			}});

	make_ajax_request({url:'ajax.admin.php?do=get_frontpage_text&language=' + language,
			callback:function(response)
			{
				_('frontpage_text').value=response;
			}});
}



kanalen.admin.load_rules_language=function(language)
{

	make_ajax_request({url:'ajax.admin.php?do=get_rules&language=' + language,
			callback:function(response)
			{
				_('rules').value=response;
			}});
}

kanalen.admin.toggle_channel=function(channel_id)
{
	
	if(this.current_channel!=false)
	{
		_('channel_' + this.current_channel).style.display="none";
	}

	if(channel_id!='false')
	{
		_('channel_' + channel_id).style.display="block";
		this.current_channel=channel_id;
	}
	else
	{
		this.current_channel=false;
	}

}


kanalen.admin.save_frontpage_video=function(form_element)
{
	
	var video_id='false';
	var channel_id='false';
	if( this.current_channel != false )
	{
		video_id=form_element['channel_videos_' + this.current_channel].value;
		channel_id=form_element['channel_id'].value;
	}

	make_ajax_request({url:'ajax.admin.php?do=set_frontpage_video&id=' + video_id + '&channelid=' + channel_id });

}

kanalen.admin.add_link=function(form_element)
{
	var description=form_element.link_description.value.toString();
	var link_text=form_element.link_text.value.toString();
	var url=form_element.link_url.value.toString();
	
	url=url.replace(/^([a-z0-9]+:\/\/)+/i,"$1");//get rid of http://http:// and the likes
	if(!url.match(/^[a-z0-9]+:\/\//))
	{
		url="http://" + url;//add protocol reference if not defined (usually http:// in that case)
	}
	
	make_ajax_request({url:'ajax.admin.php?do=add_link',
			data:
			{
				description:description,
				link_text:link_text,
				url:url
			},
			callback:function(link_id)
			{
				var table_row=document.createElement('tr');
				var desc_el=document.createElement('td');
				var link_text_el=document.createElement('td');
				var remove_el=document.createElement('td');
				desc_el.innerHTML=description;
				link_text_el.innerHTML=link_text;
				remove_el.innerHTML="<a href=\"javascript:kanalen.admin.remove_link('" + link_id + "')\">Radera</a>";
				remove_el.align='right';
				table_row.appendChild(desc_el);
				table_row.appendChild(link_text_el);
				table_row.appendChild(remove_el);
				_('link_list_table').appendChild(table_row);
			}});
}

kanalen.admin.remove_link=function(id,element)
{
	
	make_ajax_request({url:'ajax.admin.php?do=remove_link&id=' + id,callback:function()
	{
		var to_remove=element.parentNode.parentNode;//<tr><td><a><--element
		to_remove.parentNode.removeChild(to_remove);
	}});
}

kanalen.admin.remove_contact=function(id,element)
{
	make_ajax_request({url:'ajax.admin.php?do=remove_contact&id=' + id,callback:function()
	{
		var to_remove=element.parentNode.parentNode;//<tr><td><a><--element
		to_remove.parentNode.removeChild(to_remove);
	}});
}

kanalen.admin.edit_contact=function(contact_id)
{
	contact_id=make_ajax_clean(contact_id);
	make_ajax_request({url:'ajax.admin.php?do=get_contact_info&id=' + contact_id,json:true,
			callback:function(response)
			{
				var form_el=_('contact_form');
				form_el.name.value=response.name;
				form_el.position.value=response.position;
				form_el.phone.value=response.phone;
				form_el.mail.value=response.mail;
				form_el.group.value=response.groupId;
			}});

	
	this.current_contact=contact_id;
}

kanalen.admin.get_contact_list=function(page)
{
	do_ajax( _('contact_list'), 'ajax.admin.php?do=get_contact_list&page=' + page );
}

kanalen.admin.get_user_list=function(args)
{
	var page =(!args.page)? 1:args.page;
	do_ajax( _('user_list'), 'ajax.admin.php?do=get_user_list&page=' + page );
}


kanalen.admin.create_account=function(type,form_element)
{
	if(type=='channel')
	{
		var screenname=form_element.screenname.value.toString();
	}
	else
	{
		type='admin';
		var screenname='admin';
	}
	
	var username=form_element.username.value.toString()
	var password=form_element.password.value.toString()
	var confirm_password=form_element.confirm_password.value.toString()

	if(password!=confirm_password)
	{
		alert("Lösenorden stämmer inte överrens!");
		return false;
	}

	make_ajax_request({url:'ajax.admin.php?do=create_account',
			data:
			{
				type:type,
				username:username,
				password:password,
				screenname:screenname
			}});
}

kanalen.admin.get_banner_list=function(page)
{
	do_ajax(_('banner_list'), 'ajax.admin.php?do=get_banner_list&page=' + page);
}

kanalen.admin.remove_banner=function(id)
{
	id=make_ajax_clean(id);
	make_ajax_request({url:'ajax.admin.php?do=remove_banner&id=' + id,
			callback:function()
			{
				kanalen.admin.get_banner_list(1);
			}});
}


kanalen.admin.save_banner_data=function(form_element)
{

	var url=form_element.url.value.toString();
	var placement=form_element.placement.value.toString();
	var id=form_element.banner_id.value.toString();
	var banner_code="false";
	if(typeof form_element.banner_code != "undefined")
	{
		banner_code= form_element.banner_code.value.toString();
	}
	
	make_ajax_request({url:'ajax.admin.php?do=save_banner_data',
	data:{
	url:url,
	placement:placement,
	id:id,
	code:banner_code
	}});

}

kanalen.admin.remove_user=function(user_id,do_not_redirect)
{
	var that=this;
	if (confirm(kanalen.language['profile_confirm_user_will_be_deleted']))
	{
		make_ajax_request(
		{
			url:'ajax.admin.php?do=remove_user&user_id=' + user_id,
			callback:function()
			{
				if(!do_not_redirect)
				{
					document.location.href='index.php';
				}
				else
				{
					that.get_user_list({page:1});
				}
			}
		});
	}
}

kanalen.admin.activate_user=function(user_id)
{
		make_ajax_request(
		{
			url:'ajax.admin.php?do=activate_user&user_id=' + user_id
		});

}

kanalen.admin.reset_password=function(user_id)
{
		make_ajax_request(
		{
			url:'ajax.admin.php?do=reset_password&user_id=' + user_id
		});

}

kanalen.admin.add_code_banner=function(form_el)
{
	var name_identifier=form_el.name_identifier.value.toString();
	var code=form_el.banner_code.value.toString();
	
	make_ajax_request({url:'ajax.admin.php?do=add_code_banner',
	post:true,
	data:
	{
		name_identifier:name_identifier,
		code:code
	},callback:function()
	{
		kanalen.admin.get_banner_list(1);
	}
	});
}

kanalen.keep_alive=function()
{
	setInterval(function(){make_ajax_request({url:'ajax.php?action=keep_alive'});},120000);
}

kanalen.facebook_share = function()
{
	var url = document.location.href;
	window.open("http://www.facebook.com/sharer.php?u=" + encodeURIComponent(url));
}

kanalen.twitter_share = function()
{
	var url = document.location.href;
	window.open("http://twitter.com/home?status=" + encodeURIComponent(url));
}

