(function ($) {
    $.ui = $.ui || {};
    $.fn.extend({
        accordion: function (c, b) {
            var d = Array.prototype.slice.call(arguments, 1);
            return this.each(function () {
                if (typeof c == "string") {
                    var a = $.data(this, "accordion");
                    if (a) {
                        a[c].apply(a, d);
                    }
                } else if (!$(this).is(".ui-accordion")) {
                    $.data(this, "accordion", new($.ui.accordion)(this, c));
                }
            });
        },
        activate: function (a) {
            return this.accordion("activate", a);
        }
    });
    $.ui.accordion = function (e, d) {
        this.options = d = $.extend({},
        $.ui.accordion.defaults, d);
        this.element = e;
        $(e).addClass("ui-accordion");
        if (d.navigation) {
            var a = $(e).find("a").filter(d.navigationFilter);
            if (a.length) {
                if (a.filter(d.header).length) {
                    d.active = a;
                } else {
                    d.active = a.parent().parent().prev();
                    a.addClass("current");
                }
            }
        }
        d.headers = $(e).find(d.header);
        d.active = findActive(d.headers, d.active);
        if (d.fillSpace) {
            var b = $(e).parent().height();
            d.headers.each(function () {
                b -= $(this).outerHeight();
            });
            var c = 0;
            d.headers.next().each(function () {
                c = Math.max(c, $(this).innerHeight() - $(this).height());
            }).height(b - c);
        } else if (d.autoHeight) {
            var b = 0;
            d.headers.next().each(function () {
                b = Math.max(b, $(this).outerHeight());
            }).height(b);
        }
        d.headers.not(d.active || "").next().hide();
        d.active.parent().andSelf().addClass(d.selectedClass);
        if (d.event) {
            $(e).bind(d.event + ".accordion", clickHandler);
        }
    };
    $.ui.accordion.prototype = {
        activate: function (a) {
            clickHandler.call(this.element, {
                target: findActive(this.options.headers, a)[0]
            });
        },
        enable: function () {
            this.options.disabled = false;
        },
        disable: function () {
            this.options.disabled = true;
        },
        destroy: function () {
            this.options.headers.next().css("display", "");
            if (this.options.fillSpace || this.options.autoHeight) {
                this.options.headers.next().css("height", "");
            }
            $.removeData(this.element, "accordion");
            $(this.element).removeClass("ui-accordion").unbind(".accordion");
        }
    };
    function scopeCallback(a, b) {
        return function () {
            return a.apply(b, arguments);
        };
    }

    function completed(a) {
        if (!$.data(this, "accordion")) {
            return;
        }
        var b = $.data(this, "accordion");
        var c = b.options;
        c.running = a ? 0 : --c.running;
        if (c.running) {
            return;
        }
        if (c.clearStyle) {
            c.toShow.add(c.toHide).css({
                height: "",
                overflow: ""
            });
        }
        $(this).triggerHandler("accordionchange", [c.data], c.change);
    }

    function toggle(g, c, b, d, a) {
        var e = $.data(this, "accordion").options;
        e.toShow = g;
        e.toHide = c;
        e.data = b;
        var f = scopeCallback(completed, this);
        e.running = c.size() == 0 ? g.size() : c.size();
        if (e.animated) {
            if (!e.alwaysOpen && d) {
                $.ui.accordion.animations[e.animated]({
                    toShow: jQuery([]),
                    toHide: c,
                    complete: f,
                    down: a,
                    autoHeight: e.autoHeight
                });
            } else {
                $.ui.accordion.animations[e.animated]({
                    toShow: g,
                    toHide: c,
                    complete: f,
                    down: a,
                    autoHeight: e.autoHeight
                });
            }
        } else {
            if (!e.alwaysOpen && d) {
                g.toggle();
            } else {
                c.hide();
                g.show();
            }
            f(true);
        }
    }

    function clickHandler(a) {
		var c = $.data(this, "accordion").options;
        if (c.disabled) {
            return false;
        }
        if (!a.target && !c.alwaysOpen) {
            c.active.parent().andSelf().toggleClass(c.selectedClass);
            var d = c.active.next(),
            data = {
                instance: this,
                options: c,
                newHeader: jQuery([]),
                oldHeader: c.active,
                newContent: jQuery([]),
                oldContent: d
            },
            f = c.active = $([]);
            toggle.call(this, f, d, data);
            return false;
        }
        var b = $(a.target);
		/* Flash image change based on clicked header *******************************/		
		var headerText = b.text();
		if(headerText==oHeaderText.LBS.title){
			fnSlideSection(oHeaderText.LBS,{t:40,l:40,w:430});
		}else if(headerText==oHeaderText.SEOS.title){
			fnSlideSection(oHeaderText.SEOS,{t:255,l:30,w:450});
		}else if(headerText==oHeaderText.WMS.title){
			fnSlideSection(oHeaderText.WMS,{t:120,l:300,w:335});
		}else if(headerText==oHeaderText.SMM.title){
			fnSlideSection(oHeaderText.SMM,{t:20,l:30,w:480});
		}else if(headerText==oHeaderText.VMS.title){
			fnSlideSection(oHeaderText.VMS,{t:240,l:35,w:350});
		}else if(headerText==oHeaderText.LSM.title){
			fnSlideSection(oHeaderText.LSM,{t:280,l:200,w:460});
		}else if(headerText==oHeaderText.CC.title){
			fnSlideSection(oHeaderText.CC,{t:30,l:35,w:500});
		}
		/************************************************************************/
        if (b.parents(c.header).length) {
            while (!b.is(c.header)) {
                b = b.parent();
            }
        }
        var e = b[0] == c.active[0];
        if (c.running || c.alwaysOpen && e) {
            return false;
        }
        if (!b.is(c.header)) {
            return;
        }
        c.active.parent().andSelf().toggleClass(c.selectedClass);
        if (!e) {
            b.parent().andSelf().addClass(c.selectedClass);
        }
        var f = b.next(),
        d = c.active.next(),
        data = {
            instance: this,
            options: c,
            newHeader: b,
            oldHeader: c.active,
            newContent: f,
            oldContent: d
        },
        down = c.headers.index(c.active[0]) > c.headers.index(b[0]);
        c.active = e ? $([]) : b;
        toggle.call(this, f, d, data, e, down);
        return false;
    }

    function findActive(a, b) {
        return b != undefined ? typeof b == "number" ? a.filter(":eq(" + b + ")") : a.not(a.not(b)) : b === false ? $([]) : a.filter(":eq(0)");
    }
	
	function fnSlideSection(o,p) {
		$("#slide_cnt_wrap p.slide_title, #slide_cnt_wrap p.slide_content").fadeOut();
		$("#flash_images").fadeOut('slow', function() {						
			$(this).attr("src","images/"+o.image+".jpg").fadeIn('slow',function(){
				$("#slide_cnt_wrap p.slide_title").html(o.title).fadeIn();
				$("#slide_cnt_wrap p.slide_content").html(o.content).fadeIn();
			});
			$("#slide_cnt_wrap").css({top:p.t+"px",left:p.l+"px",width:p.w+"px"});
		});
    }

    $.extend($.ui.accordion, {
        defaults: {
            selectedClass: "selected",
            alwaysOpen: true,
            animated: "slide",
            event: "click",
            header: "a",
            autoHeight: true,
            running: 0,
            navigationFilter: function () {
                return this.href.toLowerCase() == location.href.toLowerCase();
            }
        },
        animations: {
            slide: function (e, d) {
                e = $.extend({
                    easing: "swing",
                    duration: 300
                },
                e, d);
                if (!e.toHide.size()) {
                    e.toShow.animate({
                        height: "show"
                    },
                    e);
                    return;
                }
                var c = e.toHide.height(),
                showHeight = e.toShow.height(),
                difference = showHeight / c;
                e.toShow.css({
                    height: 0,
                    overflow: "hidden"
                }).show();
                e.toHide.filter(":hidden").each(e.complete).end().filter(":visible").animate({
                    height: "hide"
                },
                {
                    step: function (a) {
                        var b = (c - a) * difference;
                        if ($.browser.msie || $.browser.opera) {
                            b = Math.ceil(b);
                        }
                        e.toShow.height(b);
                    },
                    duration: e.duration,
                    easing: e.easing,
                    complete: function () {
                        if (!e.autoHeight) {
                            e.toShow.css("height", "auto");
                        }
                        e.complete();
                    }
                });
            },
            bounceslide: function (a) {
                this.slide(a, {
                    easing: a.down ? "bounceout" : "swing",
                    duration: a.down ? 1000 : 200
                });
            },
            easeslide: function (a) {
                this.slide(a, {
                    easing: "easeinout",
                    duration: 700
                });
            }
        }
    });
} (jQuery));
