$(document).ready(function() {
    fixHeight();
    setScroll();
    setTimeout("extendTrack()", 350);
    $(window).resize(function() {
        fixHeight();
        setTimeout("extendTrack()", 1350);
    });
    acc();
    expalinpop();
    storysharetyper();
    //$('#size').find('img').eq(0).attr('src', '../img/smallLetterLink.png');
    $('#signUpForMails, div#close img').click(function() {
        popin();
    });
});
var openedFlag = 0;
function popin() {
    if (openedFlag == 0) {
        $('#overlay').css('height', $('body').height());
        $('#overlay, #popup').css('display', 'block');
        openedFlag = 1;
    } else {
        $('#overlay, #popup').css('display', 'none');
        openedFlag = 0;
    }
}

function extendTrack() {
    if ($('body.lps').size() < 1) {
        if ($('.jspVerticalBar').size() > 0) {
            if ($('.scrolldummy').size() == 0) $('#rightContent').append('<div class="scrolldummy">&nbsp;</div>');
        } else {
            if ($('.scrolldummy').size() > 0) $('.scrolldummy').remove();
        }
    } else {
        if ($('.jspVerticalBar').size() > 0) $('.head').css('margin-right', '-29px');
        else $('.head').css('margin-right', '0px');
    }
}
var minH = false;

function fixHeight() { //and width
    var vprt = viewportdims();
    if ($('#flashCnt').size() > 0) {
        //if (minH == false) minH = 514;
        $('#rightColl').css('height', 'auto');
        var caseIs, rcH = $('#rightColl').outerHeight();
        minH = $('#rightColl .botNav').eq(0).outerHeight() + rcH;
        //$('#rightColl, #innerContent').css('min-height',minH+'px');
        var screenHeight = vprt['height'];
        if (screenHeight > minH + 91) {
            var intermediate = screenHeight - 91;
            caseIs = 'screenHeight &gt; minH + 106';
            //$('#rightColl').css('height',(screenHeight-65)+'px');
            $('#rightColl').css('height', screenHeight + 'px');
            $('#innerContent').css('height', (screenHeight - 91) + 'px');
            $('#flashCnt').css('height', (screenHeight - 91) + 'px');
        } else {
            var intermediate = minH;
            caseIs = 'screenHeight &lt; minH + 106';
            $('#rightColl').css('height', (minH + 91) + 'px');
            $('#innerContent').css('height', minH + 'px');
            $('#flashCnt').css('height', minH + 'px');
        }
    } else {

        var productsBarHeight = 0;
        var productFix = 0;
        if ($('body').hasClass('product')) {
            productsBarHeight = 120;
            productFix = 24;
        } else {
            productsBarHeight = 0;
            productFix = 0;
        }


        var intermediate, sidebarH, rcH, caseIs = '.sidebar or #rightColl height &lt; viewport height - 91', case2 = '.sidebar &gt; #rightColl';
        if (minH == false) {
            if ($('.sidebar').size() > 0) {
                sidebarH = $('.sidebar').eq(0).outerHeight();
                minH = sidebarH;
            } else {
                $('#rightColl').css('height', 'auto');
                rcH = $('#rightColl').outerHeight();
                minH = $('#rightColl .botNav').eq(0).outerHeight() + rcH;
            }
            $('#rightColl').css('height', 'auto');
            rcH = $('#rightColl').outerHeight();
            if ($('#rightColl .botNav').eq(0).outerHeight() + rcH > minH + 26 + productsBarHeight) {
                minH = $('#rightColl .botNav').eq(0).outerHeight() + rcH - 26 - productsBarHeight;
                case2 = '#rightColl > .sidebar';
            }
        }
        if ($('.sidebar').size() > 0) $('.sidebar').eq(0).css('height', minH + 'px');
        var screenHeight = vprt['height'];
        if (minH > screenHeight - 77 - productsBarHeight) {
            intermediate = minH;
            caseIs = '.sidebar or #rightColl height &gt; viewport height - 106';
        }
        else intermediate = screenHeight - 91;
        if ($('body.lps').size() > 0) {
            $('.scroll-pane').css('height', (intermediate - 12) + 'px');
            //$('#rightColl').css('height', (intermediate+26+productsBarHeight)+'px');
            $('#rightColl').css('height', (intermediate + 91 + productsBarHeight) + 'px');
            $('.shadow2, .shadow2Inner, .shadow2InnerBlue, shadow2InnerYello, .shadow2InnerLightGreen').css('height', (intermediate + 26) + 'px');
        } else {
            $('#innerContent').css('height', (intermediate - (productsBarHeight - productFix)) + 'px');
            $('#rightContent').css('height', (intermediate - 55 - productsBarHeight + productFix) + 'px');
            //$('#rightColl').css('height', (intermediate+26)+'px');
            $('#rightColl').css('height', (intermediate + 91) + 'px');
            $('.scroll-pane').css('height', (intermediate - 55 - productsBarHeight + productFix) + 'px');
            $('.shadow2, .shadow2Inner, .shadow2InnerBlue, shadow2InnerYello, .shadow2InnerLightGreen').css('height', (intermediate + 26) + 'px');
        }
    }
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        // $('body').css({position:'static'});

        var w = vprt['width'];
        if (w > 873) $('body').css({ width: '100%' });
        if (w <= 873) $('body').css({ width: '837px' });
        //$('body').append('<div id="testinfo" style="position:absolute;width:400px;height:250px;left:50%;top:50%;margin-left:-200px;margin-top:-125px;background-color:white;border1px solid gray;"><div>');
        //$('#testinfo').html('<p>'+w+'</p>');

    } /**/
}

setScroll = function() {
    var optionset = {
        hijackInternalLinks: true,
        autoReinitialise: true,
        verticalDragMinHeight: 69,
        verticalDragMaxHeight: 69,
        hideFocus: true,
        autoReinitialiseDelay: 1000
    }
    $('.scroll-pane').jScrollPane(optionset);
}
function viewportdims() {
    this.w = 0;
    this.h = 0;
    if (!window.innerWidth) {

        if (!(document.documentElement.clientWidth == 0)) {
            this.w = document.documentElement.clientWidth;
            this.h = document.documentElement.clientHeight;
        } else {
            this.w = document.body.clientWidth;
            this.h = document.body.clientHeight;
        }
    } else {
        this.w = window.innerWidth;
        this.h = window.innerHeight;
    }
    return { 'width': this.w, 'height': this.h };
}


function acc() {
    $('.acc_container').hide(); //Hide/close all containers
    //$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

    $('.acc_trigger').click(function() {
        if ($(this).next().is(':hidden')) {
            $('.acc_trigger').removeClass('active').next().slideUp();
            $(this).toggleClass('active').next().slideDown(function() { extendTrack() });
        } else {
            if ($(this).next().is(':visible')) {
                $(this).removeClass('active').next().slideUp(function() { extendTrack() });
            }
        }
        setTimeout("extendTrack()", 1350);
        return false;

    });
}
function expalinpop() {
    $('#explainpop').bind('click', function(event) {
        var x = $(this).position().left + $(this).width();
        var y = $(this).position().top - 93;
        $('#wtfPop').css({ position: 'absolute', left: x + 'px', top: y + 'px' }).show();
    })
    $('#explainpop2').bind('click', function(event) {
        var x = $(this).position().left + $(this).width();
        var y = $(this).position().top - 93;
        $('#wtfPop2').css({ position: 'absolute', left: x + 'px', top: y + 'px' }).show();
    })

    $('#wtfPop .closewtfpop').bind('click', function() { $('#wtfPop').hide(); })
    $('#wtfPop2 .closewtfpop').bind('click', function() { $('#wtfPop2').hide(); })
}
function storysharetyper() {
    $('#writing').hide(0);
    $('#story_options').bind('change', function() {
        if ($(this).attr('value') == 0) {
            $('#writing').hide(330);
            $('#image').hide(330);
            $('#uploader').show(330);
            $('#video').css({ display: 'inline' });
        }
        if ($(this).attr('value') == 1) {
            $('#writing').show(330);
            $('#video').hide(330);
            $('#uploader').show(330);
            $('#image').css({ display: 'inline' });

        }
        if ($(this).attr('value') == 2) {
            $('#uploader').hide(330);
            $('#writing').show(330);
        }
    })
}
