﻿$(document).ready(function() {

    //BACKGROUND PIC
    ChangeBodyBackground($(".backgroundpane IMG").attr("src"));
    $(".backgroundpane IMG").remove();

    //homepage
    if ($(".homepageSet").length != 0) {
        var maxNum = $(".homepageSet").length + 1;
        var numRand = Math.floor(Math.random() * maxNum);
        numRand=numRand-1;
        $(".homepageSet").eq(numRand).addClass("fromMe");
        
        $(".hp_title STRONG").text($(".fromMe .TTL").text());
        $(".hp_text H2").text($(".fromMe .TXT_TTL").text());
        $(".hp_text H2").after($(".fromMe .TXT").html());
        ChangeBodyBackground($(".fromMe .IMG").text());
        
        $(".fromMe").removeClass("fromMe");
    }

    //GALLERY (products / companies)*******************************************************
    //first enterence
    if ($("UL.gallery").length != 0) {
        $("#images DIV:first").addClass("shownImgCollection");
        $("#images DIV.shownImgCollection A:first").addClass("shownImg");
        $("#info Div:first").css("display", "block").addClass("shownInfo");
        ChangeBodyBackground($("#images DIV.shownImgCollection A.shownImg").attr("href"));
        $("IMG.GdblLeft, IMG.GLeft").css("display", "none");
        $(".moveUp").css("display", "none");

	//roll down icon
	if($("UL.gallery LI").length<=9){
        	$(".moveDown").parent().css("display", "none");
      	}
	
	//first one blacked
	$("UL.gallery LI:first").addClass("hoveringStick");
        hoverBlack(".hoveringStick")
	//img arrows
	if($(".shownImgCollection A").length<=1){
		$("IMG.GdblRight, IMG.GRight").css("display", "none");
	}
    }

    //category scroll
    catThumbnailHeight = 47;
    catHeight = ($("UL.gallery LI").length) * catThumbnailHeight;
    catShown = catThumbnailHeight * 9;
    catBottom = (catHeight - catShown) * -1
    //-----------------
    $("IMG.moveUp").click(function() {
        $("UL.gallery").animate({ top: '+=47' }, 200, function() {
            // Animation complete.
            $(".moveDown").css("display", "block");
            if ($("UL.gallery").css("top") == "0px") {
                $(".moveUp").css("display", "none");
            }
        })
    })
    //-----------------
    $("IMG.moveDown").click(function() {
        $("UL.gallery").animate({ top: '-=47' }, 200, function() {
            // Animation complete
            $(".moveUp").css("display", "block");
            if ($("UL.gallery").css("top") == catBottom + "px") {
                $(".moveDown").css("display", "none");
            }
        })
    })


    //category hover
    $("UL.gallery LI").hover(function() {
        $(this).addClass("hovering");
        hoverBlack(".hovering")

    }, function() {
        if ($(this).hasClass("hoveringStick")) {
            //not chosen
        } else {
            $(this).removeClass("hovering");
            $(this).find("IMG").css("display", "block");
            $(this).find("SPAN.text").css("display", "none");
        }
    })

    // category clicked
    $(".gallery LI").click(function() {

        //removing any stuck hovering
        $(".gallery LI.hoveringStick").find("IMG").css("display", "block");
        $(".gallery LI.hoveringStick").find("SPAN.text").css("display", "none");
        $(".gallery LI.hoveringStick").removeClass("hoveringStick").removeClass("hovering")

        //making hover stick	
        $(this).addClass("hoveringStick");
        hoverBlack(".hoveringStick")
        //showing category
        ItemIndex = $('.gallery li').index($(this));
        //setting to default position
        $("#images DIV").removeClass("shownImgCollection");
        $("#info DIV").removeClass("shownInfo").css("display", "none");
        $("#info").css("display", "block");

        //showing clicked images and info
        $("#images DIV").eq(ItemIndex).addClass("shownImgCollection");
        $("#info DIV").eq(ItemIndex).css("display", "block").addClass("shownInfo");
        //showing first img
        $(".shownImg").removeClass("shownImg");
        $("#images DIV.shownImgCollection A").eq(0).addClass("shownImg");
        if($(".shownImgCollection A").length>=2){
		$("IMG.GdblRight, IMG.GRight").css("display", "block");
	}else{
		$("IMG.GdblRight, IMG.GRight").css("display", "none");
	}
        $("IMG.GdblLeft, IMG.GLeft").css("display", "none");

        ChangeBodyBackground($("#images DIV.shownImgCollection A.shownImg").attr("href"));
    })

    //Number Navigation
    $("#images A").click(function(event) {
        event.preventDefault();
        $(".shownImg").removeClass("shownImg");
        $(this).addClass("shownImg");
        galleryArrowShow(0);
    })

    //Arrow Navigation right
    $("IMG.GdblRight, IMG.GRight").click(function() {
        galleryArrowShow(1);
    })
    //Arrow Navigation left
    $("IMG.GdblLeft, IMG.GLeft").click(function() {
        galleryArrowShow(-1);
    })

    //Close info
    $("#info IMG.Gclose").click(function() {
        $("#info .shownInfo").slideUp('slow');
        $("#info .shownInfo H2, #info IMG.Gclose").css("display", "none");
        $("#info .h2_2, #info IMG.Gopen").show();
    })
    //Open info

    $("#info IMG.Gopen").click(function() {
        $("#info .shownInfo H2, #info IMG.Gclose").show();
        $("#info .shownInfo").slideDown('slow', function() {
            $("#info .h2_2, #info IMG.Gopen").hide()
        });

    })

    //**********************************************************************************************

//ABOUT PAGE 
   
    if ($("ul.tabs").length != 0) {
        $("ul.tabs").tabs("div.panes > div");
    }

    $(".abouttabs  LI").click(function() {
        if ($(this).index() == 1) {
            $(".contentpane .theteam").css("visibility", "visible");
        } else {
            $(".contentpane .theteam").css("visibility", "hidden");
        }

    })
    
    if($("#dnn_ctr3211_HtmlModule_lblContent").length!=0){
    	$("#dnn_ctr2847_ContentPane").next().css("display", "none"); //hidding edit for tab content so they cant ruin 
    	$("DIV.aboutText").html($("#dnn_ctr3211_HtmlModule_lblContent").html());
	$("#dnn_ctr3211_HtmlModule_lblContent").remove();
	}

    //FEEDBACK
    $(".Feedback_SectionHead, .Feedback_Instructions").remove();

    //PRESS
    $(".pressScroll").parent().parent().css("padding-bottom", "0");
    var rows = 4
    var shownCols=4
    var cols = Math.ceil($("UL.press LI").length / rows);
    var calculatedWidth = (cols * 70);
    var maxLeftMove=((cols-shownCols)*70)*-1

    if (cols <= shownCols) {
        $(".pressRight").css("display", "none");
    }

    $("UL.press").css("width", calculatedWidth);

    $("UL.press LI").hover(function() {
        $(this).addClass("hovering");
        x = $(this).find("SPAN.text").text()
        if (x.length <= 9) {
            $(this).find("SPAN.text").css("margin-top", "33px");
        }

        $(this).find("IMG").css("display", "none");
        $(this).find("SPAN.text").css("display", "block");

    }, function() {
        $(this).removeClass("hovering");
        $(this).find("IMG").css("display", "block");
        $(this).find("SPAN.text").css("display", "none");

    })

    $(".pressRight").click(function() {
        $("UL.press").animate({
            left: '-=70'
        }, 200, function() {
            // Animation complete.

            $(".pressLeft").css("display", "block");
            if ($("UL.press").css("left") == maxLeftMove +"px") {
                $(".pressRight").css("display", "none");
            }
        })
    });


    $(".pressLeft").click(function() {
        $("UL.press").animate({
            left: '+=70'
        }, 200, function() {
            // Animation complete.
            $(".pressRight").css("display", "block");
            if ($("UL.press").css("left") == "0px") {
                $(".pressLeft").css("display", "none");
            }
        });

    })


});


function ChangeBodyBackground(bg){
    $("BODY").css("background", "url(" + bg + ") no-repeat 0px 59px ");
}
 
function hoverBlack(className){
 	 x = $(className).find("SPAN.text").text()
        if (x.length <= 9) {

            $(className).find("SPAN.text").css({"margin-top":"1px", "line-height":"40px"});
        }
        $(className).find("IMG").css("display", "none");
        $(className).find("SPAN.text").css("display", "block");
 }   
    
//gallery Navigation
function galleryArrowShow(todo) {
    imgIndex = $('.shownImgCollection A').index($(".shownImg")); //current
    ImgCollectionLength = ($('.shownImgCollection A').length) - 1; //length

    if (todo != 0) {
        $("A.shownImg").removeClass("shownImg"); //remove class
        if (todo == 1) 
            imgIndex = imgIndex + 1;
        else if (todo == -1) 
            imgIndex = imgIndex - 1;
        
        $('.shownImgCollection A').eq(imgIndex).addClass("shownImg");
    }

    if (imgIndex == 0) {
        $("IMG.GdblLeft, IMG.GLeft").css("display", "none");
    } else {
        $("IMG.GdblLeft, IMG.GLeft").css("display", "block");
    }
    
    if (imgIndex == ImgCollectionLength) {
        $("IMG.GdblRight, IMG.GRight").css("display", "none");
    } else {
        $("IMG.GdblRight, IMG.GRight").css("display", "block");
    }
    
    //showing new image
 	ChangeBodyBackground($(".shownImg").attr("href"));
}
