﻿function DoCubeAction(action) {
    switch (action) {
        case 'Edit':
            document.location.href = 'MashupSave.aspx?MashupID=' + $('#CurrentCubeID').val();
            break;
        case 'Flag':
            document.location.href = 'Flagging.aspx?MashupID=' + $('#CurrentCubeID').val();
            break;
        case 'Add':
            window.parent.AddToFavoritesWrapper($('#CurrentCubeID').val());
            break;
        case 'Rate':
            
            break;
        case 'EmbedCode':
            $('#txtCubeInfo').focus();
            break;
        case 'ForumCode':
            $('#txtCubeInfo').focus();
            break;
        case 'Link':
            $('#txtCubeInfo').focus();
            break;
        case 'Email':
            document.location.href = 'TellFriend.aspx?MashupID=' + $('#CurrentCubeID').val();
            break;
        case 'Share':
            
            break;
    }
    //alert(action + ' ' + document.getElementById('CurrentCubeID').value);
}
function DoMouseOverAction(action) {
    switch (action) {
        case 'EmbedCode':
            $('#txtEmbedWeb').val($('#EmbedCode').val());
            $('#txtEmbedEmail').val($('#EmbedCodeBlog').val());
            $('#ShareInstructions').hide();
            $('#txtCubeInfo').hide();
            $('#EmbedCodeDiv').show();
            break;
        case 'ForumCode':
            $('#txtCubeInfo').val($('#ForumCode').val());
            $('#ShareInstructions').hide();
            $('#EmbedCodeDiv').hide();
            $('#txtCubeInfo').show();
            break;
        case 'Link':
            $('#txtCubeInfo').val($('#CubeLink').val());
            $('#ShareInstructions').hide();
            $('#EmbedCodeDiv').hide();
            $('#txtCubeInfo').show();
            break;
        case 'Email':
            $('#txtCubeInfo').hide();
            $('#EmbedCodeDiv').hide();
            $('#ShareInstructions').show();
            $('#ShareInstructions').html("Click the Email button to send this cube to your friends.");
            break;
        case 'Share':
            $('#txtCubeInfo').hide();
            $('#EmbedCodeDiv').hide();
            $('#ShareInstructions').show();
            $('#ShareInstructions').html("Click the Share button to post this cube to Facebook, Stumbleupon, etc.");
            break;
    }
    //alert(action + ' ' + document.getElementById('CurrentCubeID').value);
}

function SetCubeInfo() {
    $("#NumberOfCubeViews").html("cube views (" + $("#TimesViewed").val() + ")");
    $("#NumberOfEmbedViews").html('embed views (' + $("#TimesPropagated").val() + ')');
    $("#CubeNameCell").html($("#CurrentCubeName").val());
    if ($("#EnableEdit").val() == "true") {
        $("#FlagButton").hide();
        $("#EditButton").show();
    } else {
        $("#EditButton").hide();
        $("#FlagButton").show();
    }
    var ratingsReadOnly=false;
    if ($('#RatingReadOnly').val() == "true")
        ratingsReadOnly=true;

    if ($('#RatingValue').val()!="0") {
        $('.starrating:eq(' + (parseInt($('#RatingValue').val())-1) + ')').attr("checked","checked");
    }    
    $('.starrating').rating({ 
        callback: function(value, link){ 
            SaveRating(value); 
        },
        readOnly:ratingsReadOnly
    });
    $("#RatingPercentageDiv").html($("#RatingPercentage").val() +"%");
//    $('.starrating').rating('select','4')
}

function SaveRating(value) {
    $.ajax({
        type: "POST",
        url: "UtilityWebService.asmx/SaveRating",
        data: "{_value: " + value + ", _cubeID:" + $('#CurrentCubeID').val() + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(value) {
          $('.starrating').rating('readOnly', true);
          $("#RatingPercentageDiv").html(value.d +"%");
        },
        error: AjaxCallErrorHandler
    });
}

jQuery(document).ready(function() {
    //$('input.star').rating(); 
    SetCubeInfo();
    window.parent.GetCubeInfo($('#CurrentCubeID').val(), $('#CurrentCubeName').val(), $('#CubeDescription').val(), $('#PropagationImageURL').val(), $('#BrandingColor').val(), $('#BrandingImage').val(), $('#BrandingLogo').val(), $('#BrandingLink').val(), $('#BrandingRepeatStyle').val(), $('#BrandingBGPosition').val());
    if (getQueryVariable("autoadd") != null) {
        window.parent.AddToFavoritesWrapper($('#CurrentCubeID').val());
    }
    
    var object = SHARETHIS.addEntry({
	                                    title: $("#CurrentCubeName").val(),
	                                    summary: $("#CurrentCubeName").val(),
	                                    icon: $("#PropagationImageURL").val(),
	                                    url: $("#CubeLink").val()
	                                }, {button:false, popup:true} );
    var element = document.getElementById("ShareButton");
    object.attachButton(element);
});

function ShareRoutine() {
//    alert(3423);
    var linkId = document.getElementById("ShareButton");
	linkId.onclick();
}

function ChangeColor(color) {

    d = document;
    f = d.frames ? d.frames['CubeIFrame'] : d.getElementById('CubeIFrame');
    p = f.document || f.contentWindow.document;

    if (String(f) == "[object Window]") {  // ie
        f.ChangeBGColor(color);
    } else if (String(f) == "[object HTMLIFrameElement]") {  // chrome
        f.contentWindow.ChangeBGColor(color);
    }

}

//$(function(){ // wait for document to load
//    $('input.star').rating('select','1.5');
//});

