//To use call openShareModal(shareTitle, sharePath)
//Iff all reqs below are met it will show the specified modal window, with inserted share buttons
//
/*Please follow the following requirements*/
// Jquery must be available
//
// **Set the following variable in html file
// modalPath - set to path of modal html file
//
// **The modal file must have a element (prob div) of id = shareButtonHolderArea
// where the code will insert share buttons.  The nessecary id can be changed by
// setting the js variable shareButtonHolderIdInInModal to whatever the id should be
//
// requires these files, paths can be set in the variables
// jqModal.css
// jqModal.js
//

var modalPath = '';
var modalAreaID = 'modalWindowHolderArea';

var shareButtonHolderIdInInModal = 'shareButtonHolderArea';

var addThisButtonAreaHTML = ' \
<div class="addthis_default_style addthis_toolbox addthis_32x32_style"> \
    <a class="addthis_button_facebook"></a> \
    <a class="addthis_button_email"></a> \
	<a class="addthis_button_twitter"></a> \
</div>';

function getQueryVariable(variable) {
    if (location.href.indexOf('&' + variable + '=') != -1) {
        var startInd = location.href.indexOf('&' + variable + '=') + 2 + variable.length;
        var endIndex = location.href.indexOf('&', startInd);

        if (endIndex == -1)
            return location.href.substring(startInd);
        else
            return location.href.substring(startInd, endIndex);
    }
    else if (location.href.indexOf('?' + variable + '=') != -1) {
        var startInd = location.href.indexOf('?' + variable + '=') + 2 + variable.length;
        var endIndex = location.href.indexOf('&', startInd);

        if (endIndex == -1)
            return location.href.substring(startInd);
        else
            return location.href.substring(startInd, endIndex);
    }
    else
        return '';
}
function shouldRedirect() {
    if (location.href.indexOf('&fLink=') != -1)
        return true;
    else if (location.href.indexOf('?fLink=') != -1)
        return true;
    else
        return false;
}

if (shouldRedirect()) {
    var redirect = getQueryVariable('fLink');
    location.href = '/' + decodeURIComponent(redirect).replace('{hash}', '#').replace('%7Bhash%7D', '#');
}


$(document).ready(function () {
    $('<div id="' + modalAreaID + '" class="jqmWindow"></div>').appendTo('body');

    //init modal
    $('#' + modalAreaID).load(modalPath, function (response, status, xhr) {
            $('#' + modalAreaID).jqm();
     });
});

function openShareModal(shareTitle, sharePath, linkPath) {

    if (linkPath == '')
        sharePath = sharePath;
    else if (sharePath.indexOf('?') != -1)
        sharePath = sharePath + '&fLink=' + encodeURIComponent(linkPath.replace('#','{hash}'));
    else
        sharePath = sharePath + '?fLink=' + encodeURIComponent(linkPath.replace('#', '{hash}'));

    $('#' + shareButtonHolderIdInInModal).children().remove();
    $(addThisButtonAreaHTML).appendTo('#' + shareButtonHolderIdInInModal);

    addthis.toolbox('#' + shareButtonHolderIdInInModal, {}, { url: sharePath, title: shareTitle });
    $('#' + modalAreaID).jqmShow();
}


