﻿function openNewWindow(PURL, iwidth, iheight) {
    window.open(PURL, "ADF", "top=100,left=20,width=" + iwidth + ",height=" + iheight + ",scrollbars=no,resizable=no,location=0,toolbar=no,menubar=no");
}

var enableFloating = true;
var isFancyBox = false;

if (enableFloating) {
    $(window).scroll(function () {
        RightFloat();
    });

    $(window).resize(function () {
        if (window.innerHeight > 880) {
            $("#RightPanel").css("font-size", "10pt");
        } else {
            $("#RightPanel").css("font-size", "8pt");
        }
        RightFloat();
    });
}

function toggleFavorite(campfireID) {
    $(document).ready(function () {
        var Img = $('#campfire_favimg_' + campfireID);
        var imgLoc = Img.attr('src');

        if (imgLoc == '/Resources/Images/Icons/heart_yes.png') {
            Img.attr('src', '/Resources/Images/Icons/heart_no.png');

            //SET FALSE
            $.ajax({
                type: "POST",
                url: "/WebServices/AjaxCalls.asmx/SetFavourite",
                data: "{'campfire':'" + campfireID + "','fav':'false'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    //alert('test');
                    // Replace the div's content with the page method's return.
                    //$("#Result").text(msg.d);
                },

                error: function (e) {
                    alert('Error: ' + e);
                }
            });
            //
        } else {
            Img.attr('src', '/Resources/Images/Icons/heart_yes.png');
            //SET TRUE
            $.ajax({
                type: "POST",
                url: "/WebServices/AjaxCalls.asmx/SetFavourite",
                data: "{'campfire':'" + campfireID + "','fav':'true'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    //alert('test');
                    // Replace the div's content with the page method's return.
                    //$("#Result").text(msg.d);
                },

                error: function (e) {
                    alert('Error: ' + e);
                }
            });
            //
        }
    });

    return false;
}


function RightFloat() {
    var rightPanel = document.getElementById('RightPanel');
    var currentScroll = $(window).scrollTop();
    var fixedAfter = 145;

    var leftOffset = $(window).width()
    leftOffset = ((leftOffset - 980) / 2) + 670 + 10; //SUBTRACT WHOLE WRAPPER ,DIVIDE BY 2 - IS LEFT MARGIN TO WRAPPER, ADD BACK CONTENT WIDTH + 10 MORE

    var currentSetting = $(rightPanel).css('position');
    //alert(currentSetting);

    var windowHeight = window.innerHeight;
    var rightPanelHeight = rightPanel.offsetHeight;
    var contentHeight = document.getElementById('MainContent').offsetHeight;
    //console.log('Window Height: ' + windowHeight);
    //console.log('PanelHeight: ' + rightPanelHeight);
    //console.log('ContentHeight: ' + contentHeight);
    //console.log('YOffset: ' + window.pageYOffset);

    if (contentHeight > rightPanelHeight) {
        if (currentScroll >= fixedAfter) {

            if (currentSetting != 'fixed' || leftOffset != $(rightPanel).css('left')) {
                //console.log('Changing to fixed');
                $(rightPanel).css('position', 'fixed');
                $(rightPanel).css('left', leftOffset);

            }


        } else {
            if (currentSetting != 'static') {
                //console.log('Changing to static');
                $(rightPanel).css('position', 'static');
            }
        }
    }


    if (windowHeight < 780) {
        var pos1 = contentHeight - window.pageYOffset - 435;
        //console.log('Small screen');
        
        if (pos1 > 10) {
            pos1 = 10;
        }

        //console.log('Would set: ' + pos1);
        $(rightPanel).css('top', pos1 + 'px');
    }
    
}



function openFancy(URL) {
    $.fancybox({
        'href': '' + URL,
        'type': 'iframe',
        'width': '75%',
        'height': '75%',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'centerOnScroll': 'true'
    });

    return false;
}

function AddCampfireComment(CampfireID, ReplyTo) {
    var sURL = '/Campfires/Comment.aspx?ID=' + CampfireID + '&ReplyTo=' + ReplyTo;
    window.open(sURL, '', 'status=0,toolbar=0,location=0,resizable=0,width=700,height=465', true);    
    return false;
}

function EditCampfireComment(CommentID) {
    var URL = '/Campfires/Comment.aspx?ID=' + CommentID + '&Edit=Y'
    window.open(URL, '', 'status=0,toolbar=0,location=0,resizable=0,width=700,height=500', true);
    return false;
}

function AddCampfire() {
    var URL = '/Campfires/NewCampfire.aspx';
    window.open(URL, '', 'status=0,toolbar=0,location=0,resizable=0,width=700,height=500', true);
    return false;
}

function EditCampfire(CampfireID) {
    var URL = '/Campfires/NewCampfire.aspx?Edit=1&ID=' + CampfireID;
    window.open(URL, '', 'status=0,toolbar=0,location=0,resizable=0,width=700,height=500', true);
    return false;
}


//INIT SOUND MANAGER

//soundManager.url = '/Resources/soundManager/swf/'; // directory where SM2 .SWFs live

// Note that SoundManager will determine and append the appropriate .SWF file to the URL,
// eg. /path/to/sm2-flash-movies/soundmanager2.swf automatically.

// Beta-ish HTML5 audio support (force-enabled for iPad), flash-free sound for Safari + Chrome. Enable if you want to try it!
// soundManager.useHTML5Audio = true;

// do this to skip flash block handling for now. See the flashblock demo when you want to start getting fancy.
//soundManager.useFlashBlock = false;

// disable debug mode after development/testing..
// soundManager.debugMode = false;

// Option 1: Simple onload() + createSound() method

//soundManager.onload = function() {
  // SM2 has loaded - now you can create and play sounds!
//soundManager.createSound('helloWorld','/path/to/hello-world.mp3');
