﻿/* ========== HomePage Random Image ========== */
if (document.getElementById("homepage") != null) {
    
    imgHeader = new Array(21);
    imgHeader[0] = "bg001.jpg";
    imgHeader[1] = "bg002.jpg";
    imgHeader[2] = "bg003.jpg";
    imgHeader[3] = "bg004.jpg";
    imgHeader[4] = "bg005.jpg";
    imgHeader[5] = "bg006.jpg";
    imgHeader[6] = "bg007.jpg";
    imgHeader[7] = "bg008.jpg";
    imgHeader[8] = "bg009.jpg";
    imgHeader[9] = "bg010.jpg";
    imgHeader[10] = "bg011.jpg";
    imgHeader[11] = "bg012.jpg";
    imgHeader[12] = "bg013.jpg";
    imgHeader[13] = "bg014.jpg";
    imgHeader[14] = "bg015.jpg";
    imgHeader[15] = "bg016.jpg";
    imgHeader[16] = "bg017.jpg";
    imgHeader[17] = "bg018.jpg";
    imgHeader[18] = "bg019.jpg";
    imgHeader[19] = "bg020.jpg";
    imgHeader[20] = "bg021.jpg";

    index = Math.floor(Math.random() * imgHeader.length);
    sImage = imgHeader[index];
    document.getElementById('homepage').style.background = "url('images/bgcontent/" + sImage + "')";
}
else { /* do nothing */ }


/* ========== Top Menu Navigation ========== */

    ieHover = function() {
        var cssRule;
        var newSelector;
        for (var i = 0; i < document.styleSheets.length; i++)
            for (var x = 0; x < document.styleSheets[i].rules.length; x++) {
            cssRule = document.styleSheets[i].rules[x];
            if (cssRule.selectorText.indexOf("LI:hover") != -1) {
                newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
                document.styleSheets[i].addRule(newSelector, cssRule.style.cssText);
            }
        }
        var getElm = document.getElementById("nav").getElementsByTagName("LI");
        for (var i = 0; i < getElm.length; i++) {
            getElm[i].onmouseover = function() {
                this.className += " iehover";
            }
            getElm[i].onmouseout = function() {
                this.className = this.className.replace(new RegExp(" iehover\\b"), "");
            }
        }
    }
    if (window.attachEvent) window.attachEvent("onload", ieHover);


    $(document).ready(function() {
        $('#nav li.top').hover(function() {
            $('#nav li ul.sub li, #nav li ul.sub').hide();
            $('#nav li ul.sub li, #nav li ul.sub').fadeIn('fast');
        });
    });


/* ========== Sptlight ads ========== */

    if (document.getElementById('homePageSpotlights') != null) {
        MM_preloadImages('images/hp_slide01.png', 'images/hp_slide02.png', 'images/hp_slide03.png');

    /*        
        var nextAd = 0;
        var countAd = 3;
        var index = 0; imgAd = new Array(3);
        imgAd[0] = "<a href='products.aspx'><img src='images/hp_slide01.png' alt='' title='' /></a>";
        imgAd[1] = "<a href='products.aspx'><img src='images/hp_slide02.png' alt='' title='' /></a>";
        imgAd[2] = "<a href='products.aspx'><img src='images/hp_slide03.png' alt='' title='' /></a>";

        index = Math.floor(Math.random() * imgAd.length);

        document.getElementById('homePageSpotlights').innerHTML = imgAd[index];

        function getNextAd() {
            if (index + 1 == countAd) {
                index = 0;
            }
            else { index = index + 1; }

            document.getElementById('homePageSpotlights').innerHTML = imgAd[index];
            setTimeout("getNextAd()", 6000);
        }

        getNextAd();
    */
    }
    else { }
    
    function MM_preloadImages() { //v3.0
        var d = document; if (d.images) {
            if (!d.MM_p) d.MM_p = new Array();
            var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
                if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
        }

    }


    /* Inner fade */
    (function($) {

        $.fn.innerfade = function(options) {
            return this.each(function() {
                $.innerfade(this, options);
            });
        };

        $.innerfade = function(container, options) {
            var settings = {
                'animationtype': 'fade',
                'speed': 'normal',
                'type': 'sequence',
                'timeout': 2000,
                'containerheight': 'auto',
                'runningclass': 'innerfade',
                'children': null
            };
            if (options)
                $.extend(settings, options);
            if (settings.children === null)
                var elements = $(container).children();
            else
                var elements = $(container).children(settings.children);
            if (elements.length > 1) {
                $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
                for (var i = 0; i < elements.length; i++) {
                    $(elements[i]).css('z-index', String(elements.length - i)).css('position', 'absolute').hide();
                };
                if (settings.type == "sequence") {
                    setTimeout(function() {
                        $.innerfade.next(elements, settings, 1, 0);
                    }, settings.timeout);
                    $(elements[0]).show();
                } else if (settings.type == "random") {
                    var last = Math.floor(Math.random() * (elements.length));
                    setTimeout(function() {
                        do {
                            current = Math.floor(Math.random() * (elements.length));
                        } while (last == current);
                        $.innerfade.next(elements, settings, current, last);
                    }, settings.timeout);
                    $(elements[last]).show();
                } else if (settings.type == 'random_start') {
                    settings.type = 'sequence';
                    var current = Math.floor(Math.random() * (elements.length));
                    setTimeout(function() {
                        $.innerfade.next(elements, settings, (current + 1) % elements.length, current);
                    }, settings.timeout);
                    $(elements[current]).show();
                } else {
                    alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
                }
            }
        };

        $.innerfade.next = function(elements, settings, current, last) {
            if (settings.animationtype == 'slide') {
                $(elements[last]).slideUp(settings.speed);
                $(elements[current]).slideDown(settings.speed);
            } else if (settings.animationtype == 'fade') {
                $(elements[last]).fadeOut(settings.speed);
                $(elements[current]).fadeIn(settings.speed, function() {
                    removeFilter($(this)[0]);
                });
            } else
                alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
            if (settings.type == "sequence") {
                if ((current + 1) < elements.length) {
                    current = current + 1;
                    last = current - 1;
                } else {
                    current = 0;
                    last = elements.length - 1;
                }
            } else if (settings.type == "random") {
                last = current;
                while (current == last)
                    current = Math.floor(Math.random() * elements.length);
            } else
                alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
            setTimeout((function() {
                $.innerfade.next(elements, settings, current, last);
            }), settings.timeout);
        };

    })(jQuery);

    // **** remove Opacity-Filter in ie ****
    function removeFilter(element) {
        if (element.style.removeAttribute) {
            element.style.removeAttribute('filter');
        }
    }

/*
    $(document).ready(
        function() {
            $('#homePageSpotlights').innerFade({
                speed: 1000,
                timeout: 5000,
                type: 'sequence',
                containerheight: '142px'
            });
        });

*/





/* ========== #rightColumn  link fix ========== */

    if (document.getElementById('#rightColumn') != null) {

        function select_nav() {
            var nav_links = document.getElementById('#rightColumn')
             .getElementsByTagName('a');
            var selected = location.pathname;

            for (var i = 0; i < nav_links.length; i++) {
                var link = nav_links[i].pathname;

                // fiddle IE's view of the link
                if (link.substring(0, 1) != '/')
                    link = '/' + link;

                if (link == selected)
                    nav_links[i].setAttribute(cattr, 'active');
            }
        }

        window.onload = function() {
            select_nav();
        };

    }
    else { /* Do nothing */ }



/* ========== Acccordian ========== */

//    $(document).ready(function() {

//        $(".accordian div").hide();

//        $(".accordian h4").click(function() {
//            $(this).next("div").slideToggle("slow").siblings("div:visible").slideUp("fast");
//            $(this).toggleClass("active");
//            $(this).siblings("h4").removeClass("active");
//        });
//    });


    // Contact Us page - Jquery Accordian
    // requires on MasterPage = <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    $(document).ready(function() {
        //Acrtivate the first panel
        $("#firstheader").addClass("active");
        $(".accordion .cpanel:not(:first)").hide();

        //productPages Do not hide first panel
        $(".accordion2 .cpanel:first").hide();

        //productPages Do not hide first panel
        $(".accordion3 .cpanel:first").hide();

        //Toggle function. Show/hide .cpanel
        // Add/Remove ".active" to/from ".accordion h2"
        $("h2").click(function() {
            $(this).next(".cpanel").slideToggle("slow").siblings(".cpanel:visible").slideUp("slow");
            $(this).toggleClass("active");
            $(this).siblings("h2").removeClass("active");
        });

    });



/* ========== view larger Image ========== */
    
    $(document).ready(function()
     {

        //preload images. Append to body and hide.

        $('.largerImage').each(function() {
                var imgUrl= $(this).attr('href');
                var img = $('<img />')
                    .attr('src', imgUrl )
                    .load(function(){ $('body').append( $(this) ); })
                    .css({'visibility':'hidden','height':'1px'})
                ;
        });        



        //remove href from link. Links to larger image if javascript is turned off
        if ( $('.largerImage').attr('href') != null  )
        {
            $('.largerImage').removeAttr('href');
        }

        var z = 5; // for setting the z-index 
        
        $('.tnImage').click(function()
        {
           var tnImage = $(this);
           
           var parentDiv = tnImage.parent();
           var largeImgSrc = parentDiv.attr('href');          
        
           var tnHeight = $(this).attr('height');
           var tnWidth = $(this).attr('width');
           
           $(parentDiv).css({ 'width':tnWidth, 'height':tnHeight });
           
           var smalldiv = $('<div><div class="closeImg"><span>Close Image</span></div></div>');
         
                      
           $(smalldiv)
            .attr('class', 'smalldiv')
            //.addzindex()
            .ready( function (){z++;})
            .css({'opacity':'0', 'z-index' : z})
            .animate
                (
                    {
                        'opacity' : '1',
                        'width' : '500',
                        'height': '500'
                    },1000
                )
            //.css({'min-height' : tnHeight,'min-width' : tnWidth})
            .bind
                ('click', function ()
                    {
                        if ($(this).css('z-index') >= z)
                        { 
                        $(this)
                            .fadeOut(0, function()
                                {
                                    
                                    $(this).remove();        
                                    $(tnImage).show();

                                }
                            )
                            ;
                         }
                         else{
                            z++;
                            $(this).css({'z-index':z});
                         }
                         
                         z--;
                        

                    }
                )
           ;
                      
           $(parentDiv).append(smalldiv);
      
           var newImage = $('<img/>');
            $(newImage)
                .css
                ({'display' : 'none'})
                .attr('src', $(tnImage).attr('src').replace('prodImages/','prodImages/largeImage/') )
                .attr('class','largeImage')
                .fadeIn('fast')
            ;
            
            $(smalldiv).append(newImage).ready(function(){
                // Center image vertically by subtracting the height of the parent div and height of image in half.
                // and the sum becomes the top margin of the image
                // takes into account the close graphic. 25px high.
                var ph = smalldiv.height();
                var imgh = newImage.height();
                var ntm = ((ph - imgh) /2)-25; 
                newImage.css({'margin-top' : ntm });
            })
            
            ;
            
            $(tnImage).hide();
        });   
         
           
     });
     
     
     
(function ($) {
	$.fn.vAlign = function(container) {
		return this.each(function(i){
			if(container == null) {
				container = 'div';
			}
			$(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
			var el = $(this).children(container + ":first");
			var elh = $(el).height(); //new element height
			var ph = $(this).height(); //parent height
			var nh = (ph - elh) / 2; //new height to apply
			$(el).css('margin-top', nh);
		});
	};
})(jQuery);



/* ========== Map ========== */

    /**
    * ScrollView - jQuery plugin 0.1
    *
    * This plugin supplies contents view by grab and drag scroll.
    *
    * Copyright (c) 2009 Toshimitsu Takahashi
    *
    * Released under the MIT license.
    *
    * == Usage =======================
    *   // apply to block element.
    *   $("#map").scrollview();
    *   
    *   // with setting grab and drag icon urls.
    *   //   grab: the cursor when mouse button is up.
    *   //   grabbing: the cursor when mouse button is down.
    *   //
    *   $("#map".scrollview({
    *     grab : "images/openhand.cur",
    *     grabbing : "images/closedhand.cur"
    *   });
    * ================================
    */
    (function() {
        function ScrollView() { this.initialize.apply(this, arguments) }
        ScrollView.prototype = {
            initialize: function(container, config) {
                // setting cursor.
                var gecko = navigator.userAgent.indexOf("Gecko/") != -1;
                var opera = navigator.userAgent.indexOf("Opera/") != -1;
                var mac = navigator.userAgent.indexOf("Mac OS") != -1;
                if (opera) {
                    this.grab = "default";
                    this.grabbing = "move";
                } else if (!(mac && gecko) && config) {
                    if (config.grab) {
                        this.grab = "url(\"" + config.grab + "\"),default";
                    }
                    if (config.grabbing) {
                        this.grabbing = "url(" + config.grabbing + "),move";
                    }
                } else if (gecko) {
                    this.grab = "-moz-grab";
                    this.grabbing = "-moz-grabbing";
                } else {
                    this.grab = "default";
                    this.grabbing = "move";
                }

                // Get container and image.
                this.m = $(container);
                this.i = this.m.children().css("cursor", this.grab);

                this.isgrabbing = false;

                // Set mouse events.
                var self = this;
                this.i.mousedown(function(e) {
                    self.startgrab();
                    this.xp = e.pageX;
                    this.yp = e.pageY;
                    return false;
                }).mousemove(function(e) {
                    if (!self.isgrabbing) return true;
                    self.scrollTo(this.xp - e.pageX, this.yp - e.pageY);
                    this.xp = e.pageX;
                    this.yp = e.pageY;
                    return false;
                })
                .mouseout(function() { self.stopgrab() })
                .mouseup(function() { self.stopgrab() })
                .dblclick(function() {
                    var _m = self.m;
                    var off = _m.offset();
                    var dx = this.xp - off.left - _m.width() / 2;
                    if (dx < 0) {
                        dx = "+=" + dx + "px";
                    } else {
                        dx = "-=" + -dx + "px";
                    }
                    var dy = this.yp - off.top - _m.height() / 2;
                    if (dy < 0) {
                        dy = "+=" + dy + "px";
                    } else {
                        dy = "-=" + -dy + "px";
                    }
                    _m.animate({ scrollLeft: dx, scrollTop: dy },
                                "normal", "swing");
                });

                this.centering();
            },
            centering: function() {
                var _m = this.m;
                var w = this.i.width() - _m.width();
                var h = this.i.height() - _m.height();
                _m.scrollLeft(w / 2).scrollTop(h / 2);
            },
            startgrab: function() {
                this.isgrabbing = true;
                this.i.css("cursor", this.grabbing);
            },
            stopgrab: function() {
                this.isgrabbing = false;
                this.i.css("cursor", this.grab);
            },
            scrollTo: function(dx, dy) {
                var _m = this.m;
                var x = _m.scrollLeft() + dx;
                var y = _m.scrollTop() + dy;
                _m.scrollLeft(x).scrollTop(y);
            }
        };

        jQuery.fn.scrollview = function(config) {
            return this.each(function() {
                new ScrollView(this, config);
            });
        };
    })(jQuery);



    $(document).ready(function() {
        $("#clmap").scrollview({
            grab: "successes/IED-Airports-Installations-Map-v11-0109.gif",
            grabbing: "successes/IED-Airports-Installations-Map-v11-0109.gif"
        });
        
        $("#tsmap").scrollview({
            grab: "successes/IED-Transit-and-Government-Map-0509-v11.gif",
            grabbing: "successes/IED-Transit-and-Government-Map-0509-v11.gif"
        });
    });
    
    
    
    