var isMac = false, isWin = false;
try {
    isMac = navigator.appVersion.indexOf("Mac") != -1;
    // isMac = true;
    isWin = !isMac;
} catch (err) {
}

function positionPriceTag() {
    if ($('#pricetag').length == 0)
        return;
    var screenshotPos = $('#mainscreenshot').offset();
    var priceTag = $('#pricetag');
    priceTag
            .css('left', screenshotPos.left + $('#mainscreenshot').width() - 80);
    priceTag.css('top', screenshotPos.top - 30);
}

function tooltipEnter(e, parentId, offset) {
    var y = $(this).offset().top - $("#" + parentId).offset().top
            + $(this)[0].offsetHeight / 2;
    var content = $(this).attr('title');
    var id = $(this).attr('id');

    $(this).attr('title', '');

    $("#" + parentId).append(
            "<div id='tooltip' class='dynamic-tooltip'>" + content + "</div>");
    var tooltip = $("#" + parentId + " #tooltip");
    tooltip.css('opacity', 0.8);
    tooltip.css('top', y - 10);
    tooltip.css("left", offset);
    tooltip.data('trigger', id);
    tooltip.fadeIn();
}

function tooltipExit(parentId) {
    var tooltip = $("#" + parentId + " #tooltip");
    var trigger = tooltip.data('trigger');
    var content = tooltip.html();
    $('#' + trigger).attr('title', content);
    $("#" + parentId + " #tooltip").remove();
}

function addTooltips(selector, parentId, offset) {
    selector.hover(function(e) {
                tooltipEnter.call(this, e, parentId, offset);
            }, function() {
                tooltipExit(parentId);
            });
}

function safeEscape(value) {
    if (typeof(encodeURIComponent) != 'undefined')
        return encodeURIComponent(value);
    else
        return escape(value);
}

function safeUnescape(value) {
    if (typeof(decodeURIComponent) != 'undefined')
        return decodeURIComponent(value);
    else
        return unescape(value);
}

function getCookie(c_name) {
    var i,x,y,ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name)
            return safeUnescape(y);
    }
    return null;
}

$(function() {
    if ($('#pricetag').length > 0) {
        $(window).resize(function() {
            positionPriceTag();
        });
        positionPriceTag();
    }

    // Correct page header height
    var pageHeader = $('#pageheader');
    var cssHeight = parseFloat(pageHeader[0].style.height);
    if (pageHeader[0].offsetHeight != cssHeight) {
        pageHeader[0].style.height = (cssHeight + cssHeight - pageHeader[0].offsetHeight)
                + "px";
    }

    // Contact Bar Hover Effect
    $('#contact-bar a').hover(function() {
                $(this).stop().animate({
                    'margin-left' : '0'
                }, 200);
            }, function() {
                $(this).stop().animate({
                    'margin-left' : '7px'
                }, 200);
            });

    // Social Bar Hover Effect
    $('#social-bar a').hover(function() {
                $(this).stop().animate({
                    'margin-left' : '0'
                }, 200);
            }, function() {
                $(this).stop().animate({
                    'margin-left' : '-17px'
                }, 200);
            });
    $('#social-bar #google-plus-one').hover(function() {
                $(this).stop().animate({
                    'margin-left' : '0'
                }, 200);
            }, function() {
                $(this).stop().animate({
                    'margin-left' : '-17px'
                }, 200);
            });

    // Social Bar Tooltips
    addTooltips($("#social-bar a"), "social-bar", 50);
    addTooltips($("#social-bar #google-plus-one"), "social-bar", 50);
    if ($("#buy-basic-pane").length > 0) {
        addTooltips($("#buy-basic-pane a.tooltip"), "buy-basic-pane", 170);
        addTooltips($("#buy-plus-pane a.tooltip"), "buy-plus-pane", 170);
        addTooltips($("#download-trial-pane a.tooltip"), "download-trial-pane", 170);
    }

    // Photo gallery
    if (typeof ($("a[rel^='prettyPhoto']").prettyPhoto) != 'undefined') {
        $("a[rel^='prettyPhoto']")
                .prettyPhoto(
                {
                    gallery_markup : '<div class="pp_gallery">'
                            + '<a href="#" class="pp_arrow_previous">Previous</a>'
                            + '<div>'
                            + '<ul>'
                            + '{gallery}'
                            + '</ul>'
                            + '</div>'
                            + '<a href="#" class="pp_arrow_next">Next</a>'
                            + '</div>'
                });
    }

});
