var viewportWidth = jQuery(window).width();
var viewportHeight = jQuery(window).height();

function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = jQuery(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

function getPaneModuleHeight(group) {
    tallest = 0;
    group.each(function() {

        var modulesHeight = 0;
        jQuery(this).find('.container').each(function(i) {
            thisHeight += jQuery(this).height();
        });
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
        thisHeight = 0;
    });
    return (tallest);    
}

function setPaneModuleHeight(group) {
    modulesHeight = 0;
    group.each(function(i) {
        modulesHeight += parseInt(jQuery(this).height());
    });
    paneModuleHeight = getPaneModuleHeight(jQuery(".eq"));
    jQuery('.eq').height(paneModuleHeight + 55)
}

function doMenu() {
    jQuery('ul.sf-menu').superfish({
        delay: 200,                            // one second delay on mouseout 
        animation: { opacity: 'show', height: 'show' },  // fade-in and slide-down animation 
        speed: 'fast',                          // faster animation speed 
        autoArrows: false,                           // disable generation of arrow mark-up 
        dropShadows: false                            // disable drop shadows 
    });
}

function changeImage(imgIndex) {
    jQuery('.cycle').cycle(imgIndex);
    jQuery(".slideritem").removeClass("sliderhover");
    jQuery(".sliderholder .slideritem").eq(parseInt(imgIndex)).addClass("sliderhover");
}

function changeImagetab(imgIndex) {
    jQuery('.tabcycle').cycle(imgIndex);
    jQuery(".tabslideritem").removeClass("tabsliderhover");
    jQuery(".tabsliderholder .tabslideritem").eq(parseInt(imgIndex)).addClass("tabsliderhover");
}

function doCycle() {
    jQuery(document).ready(function() {
        jQuery('.cycle').cycle({
            fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            after: function() {
                var slideIndex = parseInt(jQuery(this).attr("rel"));
                jQuery(".sliderholder .slideritem").removeClass("sliderhover");
                jQuery(".sliderholder .slideritem").eq(slideIndex).addClass("sliderhover");

            }
        });
        
    });
}


function doTabCycle() {
    jQuery(document).ready(function() {

        jQuery('.tabcycle').cycle({
            fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            after: function() {
                var slideIndex = parseInt(jQuery(this).attr("rel"));
                jQuery(".tabsliderholder .tabslideritem").removeClass("tabsliderhover");
                jQuery(".tabsliderholder .tabslideritem").eq(slideIndex).addClass("tabsliderhover");

            }
        });
    });
}

function doSearch() {
    // get rid of default dnn classes and drop in the link and textbox in the styled holder
    jQuery("#SearchHolder .NormalTextBox").removeClass().appendTo(".search");
    jQuery("#SearchHolder a").removeClass().appendTo(".search");
}

function doMenuStyling() {
    // solve menu styling issue because of default superfish behavior

    jQuery('a.level0').css('color', 'white');
	jQuery('li.current a.level0').css('color', '#40a3d7');
    jQuery("div.menu li.level0:last-child").addClass("lastmenuitem");

    
    jQuery('li.level0 ul a').mouseover(function() {
        jQuery(this).closest('li.level0').find('a.level0').css('color', '#424242');
    }).mouseout(function() {
        jQuery(this).closest('li.level0').find('a.level0').css('color', 'white');
    });

    jQuery('li.level0 > a.level0').mouseover(function() {
        jQuery(this).css('color', '#424242');
    }).mouseout(function() {
        jQuery(this).css('color', 'white');
    });

}

function doInit() {
    jQuery("body").removeClass("noscroll");
    if (parseInt(viewportWidth) < 1000 && parseInt(viewportWidth) > 989) {
        jQuery("body").addClass("noscroll");
    }
    if (jQuery('.slideritem').length != 0) {

        jQuery(".slideritem").hover(
            function() {
                jQuery("a.changeimage", this).click();
                jQuery(this).addClass("sliderhover");
            },
            function() {
            //jQuery(this).removeClass("sliderhover");
            }
        );
                
        jQuery('.slideritem a').hover(function(i) {
            jQuery("a", this).click();
        });
    }
    if (jQuery('.tabslideritem').length != 0) {

        jQuery(".tabslideritem").hover(
            function() {
                jQuery("a.changeimage", this).click();
                jQuery(this).addClass("tabsliderhover");
            },
            function() {
                //jQuery(this).removeClass("sliderhover");
            }
        );

        jQuery('.tabslideritem a').hover(function(i) {
            jQuery("a", this).click();
        });
    }      
    
}

// do neccesary scripting after document is loaded
jQuery(document).ready(function() {
    doInit();
    doMenuStyling();
    doSearch();
    if (jQuery('.cycle').length != 0) {
        doCycle()
    }

    if (jQuery('.tabcycle').length != 0) {
        doTabCycle()
    }    
    
    
    equalHeight(jQuery(".eq"));
    // onclick of min/max set the correct size to all equal height columns
    jQuery('a').click(function() {
        var thisPaneModules = jQuery(this).closest('.eq').find('.container');
        setTimeout(function() {
            setPaneModuleHeight(thisPaneModules)
        }, 1850);
    });
    // onclick of plus/min in admin modules set the correct size to all equal height columns
    jQuery('input').click(function() {
        var thisPaneModules = jQuery(this).closest('.eq').find('.container');
        setTimeout(function() {
            setPaneModuleHeight(thisPaneModules)
        }, 300);
    });
});

jQuery(window).resize(function() {
    viewportWidth = jQuery(window).width();
    viewportHeight = jQuery(window).height();
    //console.log(viewportWidth);
    // scrollbar yes or no
    if (parseInt(viewportWidth) < 1000 && parseInt(viewportWidth) > 989) {
        jQuery("body").addClass("noscroll");
    }
    else {
        jQuery("body").removeClass();
    }
});


