
function ajaxUpdateDivTo(divId, theUrl){
	var indicatorId = "#" + divId + "_indicator";
	$(indicatorId).css("visibility", "visible");
    $.ajax({
        url: theUrl,
        success: function(html){
            $("#" + divId).wrapInner(html);
        },
    });
}

function ajaxSubmitAndUpdate(formId, elementId, runScript){
    var buttonId = "#" + formId + "_submit";
	var indicatorId = "#" + formId + "_indicator";
	var theUrl = $("#" + formId).attr("action");
	$(buttonId).attr("disabled", true);
	$(indicatorId).css("visibility", "visible");
	$.ajax({
		type: "POST",
		url: theUrl,
        data: $("#" + formId).serialize(),
        success: function(msg){
            $("#" + elementId).wrapInner(msg);
        },
		error: function(){
			$(buttonId).attr("disabled", false);
			$(indicatorId).css("visibility", "hidden");
			alert('Error contacting server.');
		}
    });
}

function ajaxSubmitAndUpdate2(formId, elementId1, elementId2, tourl){
    var buttonId = "#" + formId + "_submit";
	var indicatorId = "#" + formId + "_indicator";
	var thisUrl = $("#" + formId).attr("action");
	$(buttonId).attr("disabled", true);
	$(indicatorId).css("visibility", "visible");
	$.ajax({
		type: "POST",
		url: thisUrl,
        data: $("#" + formId).serialize(),
        success: function(response){
			$("#" + elementId1).wrapInner(response);
           	$.ajax({
				url: tourl,
		        success: function(html){
		            $("#" + elementId2).wrapInner(html);
		        },
				error: function(){
				} 
		    });
        },
		error: function(){
			$(buttonId).attr("disabled", false);
			$(indicatorId).css("visibility", "hidden");
			alert('Error contacting server.');
		} 
    });
}

function isSubmittingStarOrIgnore(gameId){
    for (i = 1; i <= 5; i++) {
        if (starEl(gameId, i).hasClass('submittedstar')) {
            return true;
        }
    }
    return ignoreEl(gameId).hasClass('submittedignore');
}

function starTextId(gameId){
    return '#raterText_' + gameId;
}

function starId(gameId, starNo){
    return '#star_' + gameId + '_' + starNo;
}

function starEl(gameId, starNo){
    return $(starId(gameId, starNo));
}

function starTextEl(gameId){
    return $(starTextId(gameId));
}

function starSwitch(gameId, starNo, newClass){
    el = starEl(gameId, starNo);
    el.removeClass('emptystar');
    el.removeClass('fullstar');
    el.removeClass('overstar');
    el.addClass(newClass);
}

function starRevert(gameId, failed){
    if (isSubmittingStarOrIgnore(gameId)) {
        if (!failed) 
            return;
    }
    for (i = 1; i <= 5; i++) {
        el = starEl(gameId, i);
        if (el.hasClass('ratedstar')) {
            starSwitch(gameId, i, 'fullstar');
        }
        else {
            starSwitch(gameId, i, 'emptystar');
        }
    }
    starTextRevert(gameId);
}

function starTextRevert(gameId){
    if (ignoreEl(gameId).hasClass('yesignore')) {
        starTextEl(gameId).wrapInner('Ignored');
        return;
    }
    starCount = 0;
    for (i = 1; i <= 5; i++) {
        el = starEl(gameId, i);
        if (el.hasClass('fullstar')) {
            starCount++;
        }
    }
    if (starCount > 0) {
        starTextEl(gameId).wrapInner(starCount + ' stars');
    }
    else {
        starTextEl(gameId).wrapInner('Rate it!');
    }
}

function starOver(gameId, starNo){
    if (isSubmittingStarOrIgnore(gameId)) {
        return;
    }
    i = 1;
    for (; i <= starNo; i++) {
        starSwitch(gameId, i, 'overstar');
        starTextEl(gameId).wrapInner(i + ' stars');
    }
    for (; i <= 5; i++) {
        starSwitch(gameId, i, 'emptystar');
    }
}

function starOut(gameId, starNo){
    starRevert(gameId, false);
}

function starClick(gameId, starNo){
    // check if already submit first
    if (isSubmittingStarOrIgnore(gameId)) {
        return;
    }
    for (i = 1; i <= starNo; i++) {
        starEl(gameId, i).addClass('submittedstar');
    }
    starTextEl(gameId).wrapInner("submitting...");
    $.ajax({
        url: '/user_games/rate/' + gameId + '/' + starNo,
        success: function(response){
            for (i = 1; i <= 5; i++) {
                el = starEl(gameId, i);
                if (response == "NOUSER") {
                    alert('Please sign-in to rate games.');
                    starRevert(gameId, true);
                    return;
                }
                if (el.hasClass('submittedstar')) {
                    starSwitch(gameId, i, 'fullstar');
                    el.addClass('ratedstar');
                    el.removeClass('submittedstar');
                }
                else {
                    starSwitch(gameId, i, 'emptystar');
                    el.removeClass('ratedstar');
                }
            }
            ignoreEl(gameId).removeClass('wasignore');
            ignoreSwitch(gameId, 'noignore');
            starTextRevert(gameId);
        },
        error: function(){
            alert('Error contacting server.');
            starRevert(gameId, true);
        },
    });
}

function ignoreId(gameId){
    return '#ignore_' + gameId;
}

function ignoreEl(gameId){
    return $(ignoreId(gameId));
}

function ignoreSwitch(gameId, newClass){
    el = ignoreEl(gameId);
    el.removeClass('yesignore');
    el.removeClass('noignore');
    el.removeClass('overignore');
    el.addClass(newClass);
}

function ignoreRevert(gameId, failed){
    el = ignoreEl(gameId);
    if (isSubmittingStarOrIgnore(gameId)) {
        if (!failed) 
            return;
    }
    if (el.hasClass('wasignore')) {
        ignoreSwitch(gameId, 'yesignore');
    }
    else {
        ignoreSwitch(gameId, 'noignore');
    }
    starTextRevert(gameId);
}

function ignoreOnlyOver(gameId){
    if (ignoreEl(gameId).hasClass('submittedignore') || ignoreEl(gameId).hasClass('wasignore')) {
        return;
    }
    ignoreSwitch(gameId, 'overignore');
    starTextEl(gameId).wrapInner('Ignore game');
}

function ignoreOver(gameId){
    if (isSubmittingStarOrIgnore(gameId)) {
        return;
    }
    ignoreSwitch(gameId, 'overignore');
    starTextEl(gameId).wrapInner('Ignore game');
}

function ignoreOnlyOut(gameId){
    if (ignoreEl(gameId).hasClass('submittedignore') || ignoreEl(gameId).hasClass('wasignore')) {
        return;
    }
    ignoreSwitch(gameId, 'noignore');
    starTextEl(gameId).wrapInner('');
}

function ignoreOut(gameId, starNo){
    ignoreRevert(gameId, false);
}

function ignoreOnlyClick(gameId){
    if (ignoreEl(gameId).hasClass('submittedignore')) {
        return;
    }
    
    ignoreEl(gameId).addClass('submittedignore');
    starTextEl(gameId).wrapInner("submitting...");
    
    if (!ignoreEl(gameId).hasClass('wasignore')) {
       $.ajax({
            url: '/user_games/ignore/' + gameId + '/',
            success: function(response){
                el = ignoreEl(gameId);
                el.removeClass('submittedignore');
                if (response == "NOUSER") {
                    alert('Please sign-in to rate games.');
                    ignoreRevert(gameId, true);
                    return;
                }
                if (response.responseText == "yes") {
                    ignoreSwitch(gameId, 'yesignore');
                    el.addClass('wasignore');
                    starTextEl(gameId).wrapInner("");
                }
                else {
                    ignoreSwitch(gameId, 'noignore');
                    el.removeClass('wasignore');
                }
                starTextRevert(gameId);
            },
            error: function(){
                alert('Error contacting server.');
                ignoreRevert(gameId, true);
            },
        });
    }
    else {
        $.ajax({
            url: '/user_games/ignore/' + gameId + '/',
            success: function(response){
                el = ignoreEl(gameId);
                el.removeClass('submittedignore');
                ignoreSwitch(gameId, 'noignore');
                el.removeClass('wasignore');
                starTextEl(gameId).wrapInner("");
            },
            error: function(){
                alert('Error contacting server.');
                ignoreRevert(gameId, true);
            },
        });
    }
}

function ignoreClick(gameId, starNo){
    // check if already submit first
    if (isSubmittingStarOrIgnore(gameId)) {
        return;
    }
    // if already ignore, don't do anything
    //	if (ignoreEl(gameId).hasClass('yesignore')) {
    //		return;
    //	}
    ignoreEl(gameId).addClass('submittedignore');
    starTextEl(gameId).wrapInner("submitting...");
    $.ajax({
        url: '/user_games/ignore/' + gameId + '/',
        success: function(response){
            el = ignoreEl(gameId);
            el.removeClass('submittedignore');
            if (response == "NOUSER") {
                alert('Please sign-in to rate games.');
                ignoreRevert(gameId, true);
                return;
            }
            if (response == "yes") {
                ignoreSwitch(gameId, 'yesignore');
                el.addClass('wasignore');
                for (i = 1; i <= 5; i++) {
                    starEl(gameId, i).removeClass('ratedstar');
                    starSwitch(gameId, i, 'emptystar');
                }
            }
            else {
                ignoreSwitch(gameId, 'noignore');
                el.removeClass('wasignore');
            }
            starTextRevert(gameId);
        },
        failure: function(){
            alert('Error contacting server.');
            ignoreRevert(gameId, true);
        },
    });
}

function pauseAutoTabChanges(){
    autoTabChangePaused = true;
}

var optionselected = 1;

function chooseOption(tabNo){
    if (optionselected == 0) {
        optionselected = tabNo;
        $("#optionContent" + tabNo).removeClass('vqCollapsed');
        $("#optionTab" + tabNo).addClass("selected");
    }
    else 
        if (tabNo != optionselected) {
            $("#optionContent" + optionselected).addClass('vqCollapsed');
            $("#optionContent" + tabNo).removeClass('vqCollapsed');
            $("#optionTab" + optionselected).removeClass("selected");
            $("#optionTab" + tabNo).addClass("selected");
            optionselected = tabNo;
        }
}

function collapseOptions(tabNo){
    $("#optionContent" + tabNo).addClass('vqCollapsed');
    $("#optionTab" + tabNo).removeClass("selected");
    optionselected = 0;
}

function toggleCollapse(elementId){
    el = $("#" + elementId);
    el.toggleClass('vqCollapsed');
}

var series = new Array();
var removed = new Array();

function addSeries(id){
    var indicatorId = "#serie_indicator_box";
    $(indicatorId).removeClass("ajaxIndicator");
    
    if (series.length == 5) {
        var indicatorId = "serie_indicator_box";
        $(indicatorId).addClass("ajaxIndicator");
        alert("You can only have five games in series.");
        return;
    }
    series.push(id);
    var rem_index = removed.indexOf(id);
    if (rem_index != -1) 
        removed.splice(rem_index, 1);
    sortSeries();
    showGameNotSerie();
}

function removeSeries(id){
    var indicatorId = "#serie_indicator_box";
    $(indicatorId).removeClass("ajaxIndicator");
    series.splice(series.indexOf(id), 1);
    removed.push(id);
    sortSeries();
    showGameNotSerie();
}

function swapSeriesDown(id){
    var index = series.indexOf(id);
    if (index != 0) {
        var indicatorId = "#serie_indicator_box";
        $(indicatorId).removeClass("ajaxIndicator");
        var first = series[index];
        var second = series[index - 1];
        
        series[index] = second;
        series[index - 1] = first;
        sortSeries();
    }
}

function swapSeriesUp(id){
    var index = series.indexOf(id);
    if (index != 4) {
        var indicatorId = "#serie_indicator_box";
        $(indicatorId).removeClass("ajaxIndicator");
        var first = series[index];
        var second = series[index + 1];
        
        series[index] = second;
        series[index + 1] = first;
        sortSeries();
    }
}

function sortSeries(){
    var divid = "#hns-series-toolbox";
    var url = "/hidenseek/toolbox?";
    var sep = "&";
    for (var i = 0; i < series.length; i++) {
        var atr = "game" + i + "=";
        url += atr + String(series[i]) + sep;
    }
    addtotoolbar(divid, url);
}

function showGameNotSerie(){
    var divid = "#hns-games-no-serie";
    var url = "/hidenseek/gamenoserie?";
    var sep = "&";
    for (var i = 0; i < series.length; i++) {
        var atr = "game" + i + "=";
        url += atr + String(series[i]) + sep;
    }
    for (var i = 0; i < removed.length; i++) {
        var atr = "removed" + i + "=";
        url += atr + String(removed[i]) + sep;
    }
    addtotoolbar(divid, url);
}

function addtotoolbar(divId, theUrl){
    $.ajax({
        url: theUrl,
        success: function(response){
            var indicatorId = "#serie_indicator_box";
            $(indicatorId).addClass("ajaxIndicator");
            $(divId).wrapInner(response);
        },
        failure: function(){
            alert('Error contacting server.');
        }
    });
}

function updateFilter(filter, divid, theUrl, identifier){
    theUrl = theUrl + filter;
    var indicatorId = "#" + identifier + "_indicator";
    $(indicatorId).css("visibility", "visible");
    ajaxUpdateDivTo(divid, theUrl)
}

function onOverTip(divId, title, html){
    new Ext.ToolTip({
        target: divId,
        headerAsText: false,
        hideBorders: true,
        border: false,
        bodyBorder: false,
        bodyStyle: "background-color: #FFFFFF;",
        shadow: false,
        autoWidth: true,
        autoLoad: {
            url: html
        },
        showDelay: 0,
        hideDelay: 0,
        dismissDelay: 0,
        trackMouse: true
    });
    
}

function confirmAndGoto(commentid, type){
	var data = {
		id: commentid,
		type: type
	}
	if(confirm('Are you sure you want to report this comment?')){
		$.ajax({
			type: "POST",
			url: '/reports/report_comment/',
	        data: data,
	        success: function(msg){
	            alert("Thank you for your report we will check it as soon as possible.")
	        },
			error: function(){
				alert('Error contacting server.');
			} 
	    });
	}
}

function facebook_onlogin(){
	window.location = '/index';
}

function facebook_onlogout(){
	window.location = '/users/signout';
}

function publishFeed(name, href, description, caption, fullimgpath, actiontext, target, callbackurl){
	var attachment = {
			'name':name,
			'href':href,
			'description':description,
			'caption':caption,
			'media':[{'type':'image',
     		'src':fullimgpath,
     		'href':href}]};
	var actionLink = {
			'text':actiontext,
			'href':href
	};
	if(callbackurl != null){
		FB.Connect.streamPublish('', attachment, actionLink, target, '', 
			function(post_id, exception){
				window.open(callbackurl, '_top');
			} 
		);
	}
	else{
		FB.Connect.streamPublish('', attachment, actionLink, target);
	}
	
}

function facebook_popup(url){
	//url = 'http://mysteryfotos.viquagames.com' + url;
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	if(isIE){
		window.open(url, "_blank", "width=500px,height=300px,status=0,toolbar=0,resizable=0,scrollbars=0");
	}
	else{
		window.open(url, "_blank", "width=500px,height=300px,status=0,toolbar=0,resizable=0,scrollbars=0");
	}
	
	FB.XFBML.Host.parseDomTree();
}

function facebook_submitandpublishfeed(inputid, name, href, description, caption, fullimgpath, actiontext){
	var target = $("#" + inputid).val();
	 publishFeed(name, href, description, caption, fullimgpath, actiontext, target);
}
