var currentTab = 'my_solution';
function changeQuestionSolutionTab(tabName)
{
	if(tabName == 'my_solution')
	{
		$('#question_solution_tabbed_panel').removeClass('my_question');	
		$('#question_solution_tabbed_panel').addClass(tabName);	
		currentTab = tabName;
	}
	else
	{
		$('#question_solution_tabbed_panel').removeClass('my_solution');	
		$('#question_solution_tabbed_panel').addClass(tabName);	
		currentTab = tabName;
	}
}

var tagList = new Array();
function addTagList()
{	
	var tag = $('#tag_list').val();
	var tags = tag.split(',');
	for(tagCtr=0;tagCtr<tags.length;tagCtr++)
	{
		if(jQuery.inArray(tags[tagCtr], tagList) == -1)
		{		
			tagList.push(tags[tagCtr]);
		}
	}
	$('#tag_list_display li.tag_item').remove();
	
	var tagItems = '';
	for(ctr=0;ctr<tagList.length;ctr++)
	{
		$('#tag_list_display').append('<li class="tag_item" id="tagItem'+ctr+'"><a href="javascript:void(0)" onclick="removeTagItem($(this).parent().attr(\'id\'), $(this).html())">'+tagList[ctr]+'</a></li>');	
		tagItems += tagList[ctr];
		tagItems += ctr + 1 == tagList.length ? '' : ', ';
		
		$('#tags').val(tagItems);

	}
	$('#tag_list').val('');
}

function addToTagList(tag)
{
	tagList.push(tag);	
}

function removeTagItem(tagId, tagValue)
{
	var index = tagList.indexOf(tagValue);
	tagList.splice(index, 1);

	$('#'+tagId).remove();
	
	var tagItems = '';
	
	for(ctr=0;ctr<tagList.length;ctr++)
	{		
		tagItems += tagList[ctr];
		tagItems += ctr + 1 == tagList.length ? '' : ', ';
		
		$('#tags').val(tagItems);
	}

	if(tagList.length == 0)
		$('#tags').val('');
}

function voteSolution(solutionId, questionId)
{	
	$('#solution_vote').load('/solutions/vote_solution/', {solution_id : solutionId, question_id : questionId});
	$('#voteSolution'+solutionId).load('/solutions/voteSolutionItem', {solution_id : solutionId, question_id : questionId});
}

function voteSolutionItem(solutionId, questionId, top)
{
	$('#voteSolution'+solutionId).load('/solutions/voteSolutionItem', {solution_id : solutionId, question_id : questionId});
	if(top)
		$('#voteSolution').load('/solutions/voteSolutionItem', {solution_id : solutionId, question_id : questionId});
}


function updateQuestionSection(action, value)
{			
	$('#questionSection').load('/questions/updateQuestionSection/', {action : action, value : value});
}


function updateSearchResults(action, value)
{
	$('#question_search_result').load('/search/updateSearchResults/', {action : action, value : value});
}

function udpateSearchSolutionResults(action, value)
{
	if(!$.browser.msie){
		$('#solutions').fadeOut("fast");		
	}
	
	$('#solutions').html('<div class="ajaxload"><div class="loadingcontent">&nbsp;</div></div>');
	$('#solutions').load('/search/updateSolutionSearchResults/', {action : action, value : value});
	
	if(!$.browser.msie){
		$('#solutions').fadeIn("slow");
	}
}

function updateSolutionSection(action, value)
{	
	//var w = $('#solutions').width();
	//var h = $('#solutions').height();
	if(!$.browser.msie){
		$('#solutions').fadeOut("fast");		
	}
	
	$('#solutions').html('<div class="ajaxload"><div class="loadingcontent">&nbsp;</div></div>');
	$('#solutions').load('/solutions/updateSolutionSection/', {action : action, value : value});
	
	if(!$.browser.msie){
		$('#solutions').fadeIn("slow");
	}
}

function updateCommentSection(action, value)
{
	$('#comments').load('/solutions/updateUserCommentSection/', {action : action, value : value});


}

function showReactions()
{
	$('#reaction_content_items').removeClass('hide_reactions');
	$('#show_reaction_options').addClass('hide_reactions');
	$('#hide_reaction_options').removeClass('hide_reactions');
}

function hideReactions()
{
	$('#reaction_content_items').addClass('hide_reactions');
	$('#hide_reaction_options').addClass('hide_reactions');
	$('#show_reaction_options').removeClass('hide_reactions');
}

function voteComment(commentId, value)
{
	$('#comment_vote_'+commentId).load('/solutions/vote_comment/', {comment_id : commentId, vote : value});
}

function nominateSolution(solutionId)
{
	$('#nominate_'+solutionId).load('/solutions/nominate_solution/', { solution_id : solutionId});
}

function declareWinner(solutionId, questionId)
{
	$('.winner').removeClass('declared');
	$('.solution_panel').removeClass('winner');
	$('#declare_winner_'+solutionId).load('/solutions/declare_winner/', {solution_id : solutionId, question_id : questionId });	
}

function userCheck()
{	
	$('#form_error').load('/authenticate/user_check/', { username : $('#username').val(), password : $('#password').val() });
}

function logUser(loc)
{	
	parent.parent.location = loc;
	parent.parent.$.colorbox.close();
	
}

var show_solution = true;
function showMoreSolutionDetails()
{	
	if(show_solution){	
		$('#solution_detail_description').addClass('show_description');	
		$('#solution_detail_description').removeClass('hide_description');
		$('#more_solution').html('(-)');
		show_solution = false;
		
	}	
	else
	{
		$('#solution_detail_description').addClass('hide_description');	
		$('#solution_detail_description').removeClass('show_description');
		$('#more_solution').html('(+)');
		show_solution = true;
	}
}

var show_question = true;
function showMoreQuestionDetails()
{	
	if(show_question){	
		$('#question_detail_description').addClass('show_description');	
		$('#question_detail_description').removeClass('hide_description');
		$('#more_question').html('(-)');
		show_question = false;		
	}	
	else
	{
		$('#question_detail_description').addClass('hide_description');	
		$('#question_detail_description').removeClass('show_description');
		$('#more_question').html('(+)');
		show_question = true;
	}
}

function clearTextField(id, defaultValue)
{
	if($('#'+id).val() == defaultValue)
		$('#'+id).html('');
}

function restoreTextFieldDefaultValue(id, defaultValue)
{
	if($('#'+id).val() == '')
		$('#'+id).html(defaultValue);
}

function showSolutions()
{
	$('#solutions').removeClass('hide_solutions');	
}

function changeCommentPage(page)
{
	$('#reaction_content_items').load('/solutions/updateCommentsSection/', { page : page });
}

function showPrizeOptions()
{	
	var questionId = $('#question_id') ? $('#question_id').val() : '';
	//GB_showCenter('', ('/questions/showPrizeOptions/'+questionId), 530, 945)	
	var url = '/questions/showPrizeOptions/'+questionId;
	$.colorbox({href:url, width:945, height:600, iframe:true});
}

function enableAmountOptions()
{
	if($('#amount_check').is(':checked'))
	{
		$("input[name=amount]").removeAttr('disabled');
	}
	else
	{
		$("input[name=amount]").attr('disabled', true);		
	}
	
}

function enableOtherPriceInput()
{
	defaultVal = 'Geef de prijs voor de winnaar';
	if($('#others_check').is(':checked'))
	{
		$("input[name=other_prize]").removeAttr('disabled');
		$("input[name=other_prize]").focus();
		if($("input[name=other_prize]").val() == defaultVal)
			$("input[name=other_prize]").val('');
		
	}
	else
	{
		$("input[name=other_prize]").attr('disabled', true);	
		if($("input[name=other_prize]").val() == '')
			$("input[name=other_prize]").val(defaultVal);
	}

}

var prizeAmount = '';
var prizeOther = '';

function addPrizeOptions()
{	
		
	parent.parent.$('#amount_prize').val(prizeAmount);
	parent.parent.$('#other_question_prize').val(prizeOther);
}

function showPrizeOptionsButton(val)
{
	if(val != 1)
		$('#prize_option_button').addClass('show_button');
	else
		$('#prize_option_button').removeClass('show_button');
}	

function authenticateHyvesUser(hyvesId)
{
	$('#form_error').load('/authenticate/hyves_connect/', { hyvesId : hyvesId});	
}

function showHyves()
{
	$('#hyves').removeClass('hide_hyves_button');
	networkName = 'Hyves';
}

function hideHyves()
{
	$('#hyves').addClass('hide_hyves_button');
	networkName = '';
}

function showFaq(faqItem)
{
	$('.show_faq').removeClass('show_faq');
	$('#faq'+faqItem).addClass('show_faq');	
}

function registerUserUsingHyvesAccount(username, userId)
{
	$('#social_registration_error').load('/authenticate/hyves_connect/register/', { hyves_username : username, hyves_user_id : userId});		
}

function setReferrer(referrer)
{	
	$('#referrer').load('/authenticate/referrer/', { referrer : referrer });
}

function showRegistrationform()
{
	$('#social_login').addClass('hide_social_login');
	$('#registration_form').addClass('show_registration_form');
}

var networkName = '';
function activateSocialRegistration(formAction, network)
{
	$('#social_registration_form').	attr('action', formAction);
	$('#social_login').addClass('hide_social_login');
	$('#social_registration_form').addClass('show_registration_form');
	$('#network_name').html(network);
	$('#hyves_id_error').html('');
	networkName = network;
	
	if(networkName == 'Hyves')
		$('#hyves_register').addClass('show_login_button');
	else
		$('#hyves_register').removeClass('show_login_button');
		
	$('.login_social').removeClass('hide_button');	
	$('#'+networkName+'_register_button').addClass('hide_button');
}


function logInToNetwork(networkItem, type)
{
	if(networkItem.checked)
	{
		$('#social_facebook').attr('disabled', true);
		$('#social_twitter').attr('disabled', true);
		$('#social_hyves').attr('disabled', true);
		if(type == 'question')
		{
			$('#question_form_button').attr('disabled', true);
			createCookie('question', $('#question').val());
			createCookie('day', $(".period select[name=day]").val());
			createCookie('nomination_days', $(".period select[name=nomination_days]").val());
			createCookie('question_details', $('#question_details').val());
			createCookie('tagList', tagList);
			createCookie('category', $('#category').val());
			createCookie('medium_1', $("input[name=medium[]][value=1]").attr('checked'));
			createCookie('medium_2', $("input[name=medium[]][value=2]").attr('checked'));
			createCookie('medium_3', $("input[name=medium[]][value=3]").attr('checked'));
			createCookie('medium_4', $("input[name=medium[]][value=4]").attr('checked'));
			
			createCookie('question_completion_options', $("select[name=question_completion_options]").val());
			createCookie('social_twitter', $('#social_twitter').attr('checked'));
			createCookie('social_facebook', $('#social_facebook').attr('checked'));
			createCookie('social_hyves', $('#social_hyves').attr('checked'));
			createCookie('save_question', $('#save_question').val());
			createCookie('other_question_prize', $('#other_question_prize').val());
			createCookie('amount_prize', $('#amount_prize').val());
			createCookie('question_counter', $('#question_counter').html());				
			document.location.href = '/authenticate/'+networkItem.value+'_connect/login/question_post/';		
		}
		else if(type == 'solution')
		{
			$('#solution_form_button').attr('disabled', true);
			createCookie('solution_title', $('#solution_title').val());
			createCookie('solution_description', $('#solution_description').val());
			createCookie('question_id', $('#question_id').val());
			createCookie('question_mediums', $('#question_mediums').val());
			createCookie('save_solution', $('#save_solution').val());
			createCookie('solution_counter', $('#solution_counter').html());	
			createCookie(networkItem.id, networkItem.checked);
			document.location.href = '/authenticate/'+networkItem.value+'_connect/login/solution_post/';		
		}
		else if(type == 'register')
		{
			$('#edit_account').css({'display':'none'});
			parent.parent.location.href = '/authenticate/'+networkItem.value+'_connect/login/update_account';		
		}
		
		
	}
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function populateNewQuestionCookies()
{
	$('#question_form_button').attr('disabled', false);
	$('#question').val(readCookie('question'));
	$('#question_details').val(readCookie('question_details'));
	$('#question_counter').html(readCookie('question_counter'));
	$('#tag_list').val(readCookie('tagList'));
	addTagList();	
	$(".period select[name=day] option:eq("+readCookie('day')+")").attr('selected', 'selected');
	$(".period select[name=nomination_days] option:eq("+readCookie('nomination_days')+")").attr('selected', 'selected');
	$("#category option[value="+readCookie('category')+"]").attr('selected', 'selected');
	$("select[name=question_completion_options] option[value="+readCookie('question_completion_options')+"]").attr('selected', 'selected');
	$('#social_hyves').attr('checked', readCookie('social_hyves') == 'true' ? true : false);
	$('#social_facebook').attr('checked', readCookie('social_facebook') == 'true' ? true : false);
	$('#social_twitter').attr('checked', readCookie('social_twitter') == 'true' ? true : false);	
	$('#amount_prize').val(readCookie('amount_prize'));
	$('#other_question_prize').val(readCookie('other_question_prize'));	
	$("input[name=medium[]][value=1]").attr('checked', readCookie('medium_1') == 'true' ? true : false);
	$("input[name=medium[]][value=2]").attr('checked', readCookie('medium_2') == 'true' ? true : false);
	$("input[name=medium[]][value=3]").attr('checked', readCookie('medium_3') == 'true' ? true : false);
	$("input[name=medium[]][value=4]").attr('checked', readCookie('medium_4') == 'true' ? true : false);
}

function populateNewSolutionCookies()
{
	$('#solution_form_button').attr('disabled', false);
	$('#solution_title').val(readCookie('solution_title'));
	$('#solution_description').val(readCookie('solution_description'));
	$('#social_hyves').attr('checked', readCookie('social_hyves') == 'true' ? true : false);
	$('#social_facebook').attr('checked', readCookie('social_facebook') == 'true' ? true : false);
	$('#social_twitter').attr('checked', readCookie('social_twitter') == 'true' ? true : false);	
	$('#solution_counter').html(readCookie('solution_counter'));
	$('#question_id').val(readCookie('question_id'));
	$('#question_mediums').val(readCookie('question_mediums'));
	$('#save_solution').val(readCookie('save_solution'));
}

function eraseNewQuestionCookies()
{
	var cookies = new Array('question', 'day', 'nomination_days', 'question_details', 'tagList', 'tag_list', 'category', 'medium_1', 'medium_2', 'medium_3', 'medium_4', 'question_completion_options', 'social_facebook', 'social_twitter', 'social_hyves', 'save_question', 'other_question_prize', 'amount_prize', 'question_counter');
	for(ctr=0;ctr<cookies.length;ctr++)
		eraseCookie(cookies[ctr]);	
}

function eraseNewSolutionCookies()
{
	var cookies = new Array('solution_title', 'solution_description', 'social_hyves', 'social_facebook', 'social_twitter', 'question_id', 'question_mediums', 'save_solution');	
	for(ctr=0;ctr<cookies.length;ctr++)
		eraseCookie(cookies[ctr]);	
}

var req;
function showNetworkInfo(network, networkUserId, itemId, type)
{
		$('#'+type+network+'Info_'+itemId).css('display:inline-block; background:#fff; width:200px; border:1px solid #666;');
	//$('#solution'+network+'Info_'+solutionId).load('/ajax/renderSocialInfo/', { network : network, network_user_id : networkUserId });	
	//$('#solution'+network+'Info_'+solutionId).html('<img src="/assets/images/loading.gif" alt="loading..." width="10" height="10" />');
	if(network != 'hyves')
	{
	req = $.ajax({
					 type: "POST",
					 url: '/ajax/renderSocialInfo/',
					 data: 'network='+network+'&network_user_id='+networkUserId,
					 //onLoading: $('#solution'+network+'Info_'+solutionId).html('<img src="/assets/images/loading.gif" alt="loading..." width="30" height="30" />'),
					 success: function(msg){
						 $('#'+type+network+'Info_'+itemId).html(msg);
					 	}
					 });
	}
	else
	{
		req = $.ajax({
					 type: "POST",
					 url: '/hyves/user_info.php?hyves_id='+networkUserId,
					 //data: 'network='+network+'&network_user_id='+networkUserId,
					 //onLoading: $('#solution'+network+'Info_'+solutionId).html('<img src="/assets/images/loading.gif" alt="loading..." width="30" height="30" />'),
					 success: function(msg){
						 $('#'+type+network+'Info_'+itemId).html(msg);
					 	}
					 });
	}

}

function abortShowSolutionNetworkInfo(network, solutionId)
{
//	$('#solution'+network+'Info_'+solutionId).abort();
	//currentRequest.abort();
	//req.abort();
}

function delete_account(userId)
{
	$('#delete_account').load('/account/delete_account/', { user_id : userId });	
	hide_edit_account_form();
}

function hide_edit_account_form()
{
	$('#edit_account_form').css('display', 'none');	
}

function openColorBox(class_name, popup_width, popup_height)
{
	$("."+class_name).colorbox({width:popup_width, height:popup_height, iframe:true});
}

function openPopupWindow(url, popup_width, popup_height, redirect)
{
	$.colorbox({href:url, width:popup_width, height:popup_height, iframe:true, onClosed:function(){ 
		document.location.href = redirect;
	}});
}
