function getUrlVarsFrom(url) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function setTools() {

    if (getUrlVarsFrom(window.location.href)['categoryid'] === '5199') {
        $('#objectTools').css('display', 'none')
    }

}

function toggleInfo() {

    var $itemContent = $("#longDescription");
    $itemContent.hide();

    $('#information').click(function (e) {
        e.preventDefault();
        $itemContent.slideToggle('slow');
    });
}

function getElements() {

    var address = $('.descUrl').attr("href");
    $('div#objectTools').addClass('loading');
    $("<div>").load(address + ' div#objectTools', function () {
        $('div#objectTools').append($(this).find('div#objectTools').html());

        $('div#objectTools').removeClass('loading');
    });

    $("<div>").load(address + ' div#longDescription', function () {
        $('div#longDescription').append($(this).find('div#longDescription').html());
        toggleInfo();


    });


}

function fixTitle() {
    var title = $('h1 > span').text().split('(');
    if (title[1]) {
        var date = '<span class="date">' + title[1].replace(')', '') + '</span>'
    } else var date = '';
    $('h1 > span').replaceWith(title[0] + date);
}


$(document).ready(function () {

    getElements();
    fixTitle();

    if ($('.previousImage').attr("href")) {
        $('.previous').click(function () {
            window.location = $('.previousImage').attr("href");
            return false;
        })
    } else $('.previous').css('display', 'none');
    if ($('.nextImage').attr("href")) {
        $('.next').click(function () {
            window.location = $('.nextImage').attr("href");
            return false;
        })
    } else $('.next').css('display', 'none');

    $('.descUrl').children().click(function () {
        $('.fullScreen').click()
    });
    $('.descUrl').replaceWith($('.descUrl').children());


    setTools();



});





function openPrint(objectID, dealerID) {
    window.open('PrintObject.aspx?objectid=' + objectID + '&dealerid=' + dealerID, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,width=700,height=600,top=0,left=0, menubar=no, location=yes');
}

function openPrintPDF(objectID, dealerID) {
    window.open('PrintObjectPDF.aspx?objectid=' + objectID + '&dealerid=' + dealerID, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,width=700,height=600,top=0,left=0, menubar=no, location=yes');
}

function openPrintStickers(objectID, dealerID) {
    window.open('PrintObjectStickers.aspx?objectid=' + objectID + '&dealerid=' + dealerID, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,width=700,height=600,top=0,left=0, menubar=no, location=yes');
}

function openPrintStickersPDF(objectID, dealerID) {
    window.open('PrintObjectStickersPDF.aspx?objectid=' + objectID + '&dealerid=' + dealerID, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,width=700,height=600,top=0,left=0, menubar=no, location=yes');
}

function openFullScreen(fullScreenURL) {
    if (fullScreenURL.Length != 0) {
        if ((navigator.platform.indexOf('Mac') != -1) || (navigator.userAgent.indexOf('MSIE') == -1)) //mac 
        {
            var w = screen.width;
            var h = screen.height;

            window.open('FullScreenZoom.aspx?photos_zoom=' + fullScreenURL, '_blank', 'toolbar=no,scrollbars=no,resizable=no,width=' + w + ',height=' + h + ',top=0,left=0, menubar=no');
        }
        else {
            var w = screen.width - 10;
            //var h = screen.height
            var hpercent = screen.height * 91 / 100;
            var h = hpercent;

            window.open('FullScreenZoom.aspx?photos_zoom=' + fullScreenURL, '_blank', 'toolbar=no,scrollbars=no,resizable=no,width=' + w + ',height=' + h + ',top=0,left=0, menubar=no, status=yes');

            //window.open('FullScreenZoom.aspx?photos_zoom=' + fullScreenURL, '_blank', 'toolbar=no,scrollbars=no,resizable=no,fullscreen=yes');
        }
    }
}

function OpenEnlarge(photoURL, winName) {
    window.open('PressEnlargeHighlight.aspx?photopath=' + photoURL, '_blank', 'titlebar=no,menubar=no,toolbar=no,scrollbars=no,resizable=no,width=680,height=680,left=200,top=250');
}

function openContactUs(contactUsURL) {
    window.open(contactUsURL, '_blank', 'toolbar=no,scrollbars=no,resizable=no,width=600px,height=630px');
}

function OpenZoom(zoomURL, winName) {
    if ((navigator.platform.indexOf('Mac') != -1) || (navigator.userAgent.indexOf('MSIE') == -1)) //mac 
    {
        var w = screen.width;
        var h = screen.height;

        window.open(zoomURL, '_blank', 'toolbar=no,scrollbars=no,resizable=no,width=' + w + ',height=' + h + ',top=0,left=0, menubar=no');
    }
    else {
        window.open(zoomURL, '_blank', 'toolbar=no,scrollbars=no,resizable=no,fullscreen=yes');
    }
}
