$J = jQuery.noConflict();

var lightBox = {
    init : function(src, width, height, iframe) {
        if (iframe == undefined) {
            iframe = false;
        }
        if (iframe) {
            var settings = {
                // pull up link in an iframe
                message : '<iframe id="iframe" name="studyFrame" width="' + width + '" height="' + height + '" frameborder="0" src="' + src + '"></iframe><a href="#" id="windowClose"><img src="http://thegrandriver.com/images/grandriver/ourwork/lightBox_close.gif" width="30" height="11" alt="CLOSE" /></a><a id="allWork" href="/our-work"><img src="http://thegrandriver.com/images/grandriver/ourwork/lnk_all_work.gif" width="106" height="18" alt="SEE ALL OUR WORK" /></a>',
                css : {
                    border : "7px solid #a4a4a4",
                    left : ($J(window).width() - width) /2 + 'px',
                    top : ($J(window).height() - height) /2 + 'px',
                    width : width + 'px',
                    height : height + 'px'
                }
            }
        }
        else {
            var settings = {
                message : '<div id="frame"></div><a id="windowClose"><img src="http://thegrandriver.com/images/grandriver/ourwork/lightBox_close.gif" width="30" height="11" alt="CLOSE" /></a>',
                css : {
                    border : "7px solid #a4a4a4",
                    left : ($J(window).width() - width) /2 + 'px',
                    top : ($J(window).height() - height) /2 + 'px',
                    width : width + 'px',
                    height : height + 'px'
                }
            }
        }
        $J.blockUI(settings);
        $J(".blockOverlay, #windowClose").click(function() {
            lightBox.close();
            return false;
        });
        $J("#allWork img").css('top', '0px');
        $J("#allWork:hover img").css('top', '-9px');
		
		
        // listen for escape key to close window
        $J(document).keypress(function (e) {
            if (e == null) { // ie
                keycode = e.keyCode;
            } else { // mozilla
                keycode = e.which;
            }
            if (keycode == 0 && e.keyCode != 0) {
                keycode = e.keyCode;
            }
            if (keycode == 27) {
                $J("#windowClose").click();
            }
        });
    },
    close : function() {
        $J.unblockUI();
    }
};

$J(document).ready(function () {

    if ($J("body#clients").length > 0) {
		
        // apply blockUI window functionality to thickbox links on clients page
        $J("a.thickbox").click(function() {
            var href = $J(this).attr("href");
            lightBox.init(href,870,660,true);
            return false;
        });
				
        // apply hover animation to client images
        $J(".hasCase").hoverIntent(
            function () {
                var position = $J(this).position();
                if ($J(this).next().hasClass("blurbLeft")) {
                    $J(this).next().css("left",(position.left-250)+"px");
                } else {
                    $J(this).next().css("left",(position.left+175)+"px");
                }
                $J(this).next().css("top",position.top+"px");
                $J(this).next().fadeIn("slow");
            },
            function () {
                $J(this).next().fadeOut("fast");
            }
            );
		
    } // end body#clients
	
    if ($J("body#ourwork").length > 0) {
	
        var flashvars = {
            varXmlPath: "/index.php/xml/"
        };
        var params = {
            wmode: "opaque"
        };
        var attributes = {};
        swfobject.embedSWF("/flash/ourwork/ourwork.swf", "FlashID", "995", "490", "9.0.0","/flash/expressInstall.swf", flashvars, params, attributes);
		
        if(location.search.match(/varClient/)) {
            var caseStudy = location.search.split('=');
            var client = caseStudy[1];
            lightBox.init('/case-study/' + client, 870, 660, true);
        }
    } // end body#ourwork

    if ($J("body#whatwedo").length > 0) {
	
        var flashvars = {
            xmlPath: "/index.php/xml/gri_whatwedo.xml"
        };
        var params = {
            wmode: "opaque"
        };
        var attributes = {};
        swfobject.embedSWF("/flash/whatwedo/whatwedo.swf", "FlashID", "828", "351", "9.0.0","/flash/expressInstall.swf", flashvars, params, attributes);
		
    }; // end body#whatwedo


	
    if ($J("body#techlab").length > 0) {
	
        initLab = function () {
			
            // initialize scrolling thumbnail gallery
            $J("#thumbsWrap").scrollable({
                items: "#thumbs",
                nextPage: "#pageRight",
                prevPage: "#pageLeft",
                size: 3
            });
			
        }();
		
    }; // end body#techlab
	
    if ($J("body.thickbox").length > 0) {
		
        // apply blockUI window functionality to thickbox links on widgets page
        $J("a.thickbox").click(function() {
            var href = $J(this).attr("href");
            lightBox.init(href,818,465,true);
            return false;
        });
    }; // end body.thickbox
	
    if ($J("body#whoweare").length > 0) {
	
        initLab = function () {
			
            // initialize scrolling thumbnail gallery
            $J("#thumbsWrap").scrollable({
                items: "#thumbs",
                nextPage: "#pageRight",
                prevPage: "#pageLeft",
                size: 3
            });
			
        }();
		
    }; // end body#whoweare
	
    if ($J("body#careers").length > 0) {
        init = function () {
            if (!this.careersSubmit) {
                $J("#career_nav").tabs();
                $J(".career_tabs").tabs();
            }
            else {
                $J("#career_nav").tabs();
                $J(".career_tabs").tabs();
                $J("#career_nav").tabs('select', this.tabIndex);
                $J(".career_tabs").eq(this.tabIndex).tabs('select', 3);
                this.careersSubmit = false;
            }
        }();

        // don't display "What it's like Here" nav link
        $J(".tab_nav").children("li:eq(0)").css("display", "none");
		
    }; // end body#careers
	
    if ($J("body#leadership").length > 0) {
        init = function () {
			
            $J('.leadership_bio').each(function(i) {
                var details = $J('.leadership_details')[i];
                if ($J(this).height() > $J(details).height()) {
                    $J(details).height($J(this).height());
                }
            });
             // initialize scrolling thumbnail gallery
            $J("#thumbsWrap").scrollable({
                items: "#thumbs",
                nextPage: "#pageRight",
                prevPage: "#pageLeft",
                size: 5
            });
            $J("#thumbsWrap").tabs();
            if (location.hash) {
                scroll(0,0);
                $J('.thumb').each(function(i) {
                    var href = this.href.split('#');
                    href = '#'+href[1];
                    if ((href) == location.hash) {
                        if (i >= 4) {
                            var api = $J("#thumbsWrap").data("scrollable");
                            api.end();
                        }
                    }
                });
            }

        }();
    }; // end body#leadership

    if ($J("body#contact").length > 0) {
        $J("#upload").hide();
        $J(":checkbox").attr('checked', false);
        $J("#employment").change(function() {
            if(this.checked == true) {
                $J("#upload").show();
            }
            else {
                $J("#upload").hide();
            }
        });
    }
    
    if ($J("body#results, body#noresults").length > 0) {
        if ($J('#bodySidebar').height() > $J('#bodyMain').height() && $J("body#leadership").length == 0) {
            $J('#bodyMain').height($J('#bodySidebar').height());
        }
    }

		$J('a').click(function(){
			if(this.rel.match('external')) {
				var newWindow = window.open(this.href);
				newWindow.focus();
				return false;
			}
		});
});