// JavaScript Document
(function(){
    var $asiance, $body, $contact, $container, $features, $home, $intlinks, $lang, $livestream, $load, $menu, $menulinks, $panes, $project, $showhide, $window, $workimages, contact_loaded, disable_modal, options, resized, scrollElement, timer_track_page;
    $load = $("#loading");
    
    $(window).load(function(){
        return $load.delay(3000).fadeOut(1000);
        
    });
    
    window.lang = $("html").attr("lang");
    window.current_page = "";
    
    if ($.browser.webkit) {
        scrollElement = "body";
    }
    else {
        scrollElement = "html";
    }
    
    /*Jquery UI Modal Config*/
    options = {
        modal: true,
        width: 780,
        height: 'auto',
        closeText: "",
        draggable: false,
        resizable: false,
        hide: "fade",
        show: "fade",
        position: ["center", "center"],
        cur: 0,
        open: function(){
            return options.cur = 0;
        }
    };
    
    $asiance = $(".logo-site-name", $("#header"));
    $panes = $(".view-view-home", $("#content"));
    $container = $("#content");
    $home = $("#home", $("#content"));
    $project = $("#porto .views-row");
    $features = $home.find("ul li");
    $lang = $("#lang");
    $menu = $("#menu");
    $menulinks = $("#menu a");
    $intlinks = $("nav a[href^=#]");
    $showhide = $(".showhide .item-list li");
    $window = $(window);
    $body = $("body");
    timer_track_page = 0;
    contact_loaded = 0;
    disable_modal = 1;
    resized = "0";
    
    
    
    window.Asiance = {
        initialize: function(){
            var didResize, self;
            self = this;
            if (typeof document.location.hash.split("#")[1] === "undefined") {
                window.current_page = "home";
            }
            else {
                window.current_page = document.location.hash.split("#")[1];
            }
            self.init_asiance();
            self.detect_resolution();
            self.resize();
			if (navigator.userAgent.match(/iPad/i)) {
				window.iPad = true;
				return self.init_ipad();
			} else {
				self.init_browsers();
				self.reposition();
				didResize = false;
				$(window).resize(function(){
					return didResize = true;
				});
				setInterval((function(){
					if (didResize) {
						didResize = false;
						self.detect_resolution();
						self.resize();
						return self.reposition();
					}
				}), 250);
				$window.load(function(){
					return self.animate_menu();
				});
				return $(document).keydown(self.key_nav);
			}
            
            
        },
        update_hash: function(){
            return document.location.hash = "#" + window.current_page;
        },
        init_asiance: function(){
            $("#about li").not(":first").find(".body").hide().end();
            $("#business li").not(":first").find(".body").hide().end();
            $home.find(".views-row-1 .title").bind("click", function(){
                $("#modal1").dialog(options);
                $("#modal1").dialog("open");
                return false;
            });
            $home.find(".views-row-2 .title").bind("click", function(){
                $("#modal0").dialog(options);
                $("#modal0").dialog("open");
                return false;
            });
            $home.find(".views-row-3 .title").bind("click", function(){
                $("#modal2").dialog(options);
                $("#modal2").dialog("open");
                return false;
            });
            $home.find(".views-row-4 .title").bind("click", function(){
                $("#modal3").dialog(options);
                $("#modal3").dialog("open");
                return false;
            });
            
            return $showhide.addClass("cross").children(".title").bind("click", function(){
                var $this;
                $this = $(this);
                if ($this.parent("li").find(".body").attr("style") == "display: none;") {
                    return $this.parent("li").toggleClass("cross").find(".body").slideToggle("slow").end().siblings().addClass("cross").find(".body").hide("slow");
                }
            });
        },
        key_nav: function(event){
            if (event.which === 37) {
                event.preventDefault();
                return $("#menu .active").prev().click();
            }
            else 
                if (event.which === 39) {
                    event.preventDefault();
                    return $("#menu .active").next().click();
                }
        },
        init_browsers: function(){
            var self;
            self = this;
            $window.bind("hashchange", function(event){
                event.preventDefault();
                return false;
            });
            $intlinks.bind("click", function(event){
                var $anchor;
                event.preventDefault();
                $anchor = $(this);
                $(scrollElement).stop().animate({
                    scrollLeft: $("#" + $anchor.attr("title")).offset().left
                }, 600, "swing", function(){
                    return self.make_active();
                });
                clearTimeout(timer_track_page);
                window.current_page = $anchor.attr("title");
                //timer_track_page = setTimeout("window.Asiance.track_page()", 2000);
                return setTimeout("window.Asiance.update_hash()", 1000);
                //$(".modal").dialog("close");
            });
            $(scrollElement).bind("mousewheel", function(event, delta){
                if (delta > 0) {
                    $(".active").prev().click();
                    event.preventDefault();
                }
                else {
                    $(".active").next().click();
                    event.preventDefault();
                }
                return false;
            });
            return $features.hover((function(){
                $(this).find(".nothing").stop(true, true).animate({
                    height: "100%"
                });
                return $(this).find(".title").fadeIn('slow');
            }), function(){
                $(this).find(".nothing").stop(true, true).animate({
                    height: "0"
                });
                return $(this).find(".title").fadeOut('fast');
            });
        },
		init_ipad: function() {
		  var loaded, self;
		  self = this;
		  loaded = function() {
			var currentPage, myScroll;
			currentPage = 0;
			myScroll = new iScroll("scroller", {
			  hScrollbar: false,
			  vScrollbar: false,
			  vScroll: false,
			  snap: ".pane",
			  snapThreshold: 70,
			  momentum: false,
			  bounce: false,
			  lockDirection: true,
			  useTransition: true,
			  onScrollEnd: function() {
				$(".active").removeClass("active");
				$("#menu a:nth-child(" + (this.currPageX + 1) + ")").addClass("active");
				clearTimeout(timer_track_page);
				window.current_page = $("#menu a:nth-child(" + (this.currPageX + 1) + ")").attr("title");
				timer_track_page = setTimeout("window.Asiance.track_page()", 2000);
				return currentPage = this.currPageX;
			  }
			});
			$menulinks.bind("click", function(event) {
			  var pagenumber;
			  pagenumber = $(this).index();
			  myScroll.scrollToPage(pagenumber, 0, 1000);
			  return event.preventDefault();
			});
			$asiance.bind("click", function(event) {
			  myScroll.scrollToPage(0, 0, 1000);
			  return event.preventDefault();
			});
			$body.bind("touchmove", function(event) {
			  return event.preventDefault();
			});
			$window.bind("orientationchange", function(event) {
			  $load.show();
			  self.detect_orientation();
			  self.resize();
			  myScroll.refresh();
			  myScroll.scrollToPage(currentPage, 0, 200);
			  return $load.delay(1000).fadeOut();
			});
			return true;
		  };
		  $body.addClass("ipad");
		  $container.wrap("<div id=\"scroller\" />");
		  self.detect_orientation();
		  self.asiance_map();
		  self.make_modal();
		  return $window.load(loaded);
		},
        make_modal: function(){
            return $project.each(function(index){
                return $(".img-high" + index).click(function(){
                    $("#modal" + index).dialog(options);
                    $("#modal" + index).dialog("open");
                    return false;
                });
            });
        },
        make_active: function(){
            var $link, current, hash, self;
            self = this;
            current = self.get_clickables()[1];
            hash = current[0].id;
            $link = $menulinks.filter("[href=#" + hash + "]");
            if ($link.length && !$link.is(".active")) {
                $menulinks.removeClass("active");
                $link.addClass("active");
            }
            if (hash === "contact" && contact_loaded === 0) {
                self.asiance_map();
                contact_loaded = 1;
            }
            if (hash === "porto" && disable_modal === 1) {
                self.make_modal();
                return disable_modal = 0;
            }
            
        },
        animate_menu: function(){
            var $el, $magicLine, $magicLine2, leftPos, newWidth;
            $el = void 0;
            leftPos = void 0;
            newWidth = void 0;
            $menu.append("<span id=\"magic\"></span><span id=\"magic2\"></span>");
            $magicLine = $("#magic");
            $magicLine2 = $("#magic2");
            $magicLine.width($("#menu .active").outerWidth(true)).css("left", $("#menu .active").position().left);
            $magicLine2.width($("#menu .active").outerWidth(true)).css("left", $("#menu .active").position().left).data("origLeft", $magicLine.position().left).data("origWidth", $magicLine.width());
            $menulinks.hover((function(){
                $el = $(this);
                leftPos = $el.position().left;
                newWidth = $el.outerWidth(true);
                return $magicLine2.stop().animate({
                    left: leftPos,
                    width: newWidth
                });
            }), function(){
                return $magicLine2.stop().animate({
                    left: $magicLine2.data("origLeft"),
                    width: $magicLine2.data("origWidth")
                });
            });
            return $("nav a[href^=#]").click(function(){
                var $magic, target;
                target = $(this).attr("title");
                $magic = $menulinks.filter("[href=#" + target + "]");
                leftPos = $magic.position().left;
                newWidth = $magic.outerWidth(true);
                $magicLine2.data("origLeft", leftPos).data("origWidth", newWidth);
                $magicLine2.stop().animate({
                    left: leftPos,
                    width: newWidth
                });
                return $magicLine.stop().animate({
                    left: leftPos,
                    width: newWidth
                });
            });
        },
        asiance_map: function(){
            var latlng, map, mapOptions, marker, p, rc, rv;
            latlng = new google.maps.LatLng(-6.205664, 106.821309);
            mapOptions = {
                zoom: 15,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            marker = new google.maps.Marker({
                position: latlng,
                map: map,
                icon: "http://mitora.com/css/img/map_marker.png"
            });
            return google.maps.event.addListener(marker, "click", function(){
                return window.open("http://maps.google.com/maps?saddr=&daddr=Wisma+Kyoei+Prince+Jakarta+10220,+Indonesia");
            });
        },
        
        get_clickables: function(){
            var elem_left, elem_right, paneR, scrollX, viewR, visibles, width;
            width = $window.width();
            scrollX = $(window).scrollLeft();
            viewR = scrollX + $(window).width();
            elem_left = $panes.eq(Math.floor(scrollX / width));
            elem_right = $panes.eq(Math.floor(viewR / width));
            visibles = void 0;
            if (elem_right[0]) {
                if (elem_left[0].id === elem_right[0].id) {
                    visibles = elem_left;
                }
                else {
                    visibles = elem_left.nextUntil(elem_right).andSelf().add(elem_right);
                }
            }
            else {
                visibles = elem_left;
            }
            if (visibles.size() > 2) {
                return [[visibles.first(), visibles.last()], visibles.eq(1)];
            }
            if (visibles.size() === 2) {
                paneR = visibles.eq(1).offset().left + visibles.eq(1).outerWidth();
                if (visibles.first().offset().left - scrollX < viewR - paneR) {
                    return [[visibles.first()], visibles.eq(1)];
                }
                else {
                    return [[visibles.eq(1)], visibles.first()];
                }
            }
            return [[], visibles.first()];
        },
        detect_orientation: function(){
            if (orientation === 0) {
                return $body.addClass("portrait").removeClass("landscape small");
            }
            else 
                if (orientation === 90) {
                    return $body.removeClass("portrait").addClass("landscape");
                }
                else 
                    if (orientation === -90) {
                        return $body.removeClass("portrait").addClass("landscape");
                    }
                    else {
                        if (orientation === 180) {
                            return $body.addClass("portrait").removeClass("landscape small");
                        }
                    }
        },
        detect_resolution: function(){
            if (($window.width() >= "1008") && ($window.height() >= "679")) {
                $body.removeClass();
            }
            else {
                $body.addClass("small");
            }
            if (($window.width() < "1000") && ($window.width() <= $window.height())) {
                return $body.addClass("portrait");
            }
            else {
                return $body.removeClass("portrait");
            }
        },
        resize: function(){
            var $magicLine, $magicLine2, pageH, pageW, paneH;
            pageH = $window.height();
            pageW = $window.width();
            paneH = pageH;
            if ((navigator.appVersion.indexOf("MSIE 7.") !== -1) && (resized === "0")) {
                $panes.width(pageW).height(paneH - 18);
                $container.width($window.width() * 5);
                $home.width(pageW + 60).height(paneH - 3);
                resized = 1;
            }
            else {
                $panes.width(pageW).height(paneH);
                return $container.width($window.width() * 5);
                
            }
            
        },
        
        reposition: function(){
            var self;
            self = this;
            if (document.location.hash !== "") {
                $(scrollElement).stop().scrollLeft($(document.location.hash).offset().left);
                $menulinks.filter("[href=#home]").removeClass("active");
                $menulinks.filter("[href=" + document.location.hash + "]").addClass("active");
                if (document.location.hash === "#contact" && contact_loaded === 0) {
                    self.asiance_map();
                    contact_loaded = 1;
                }
                if (document.location.hash === "#porto" && disable_modal === 1) {
                    self.make_modal();
                    return disable_modal = 0;
                }
                
            }
        }
    };
    "Asiance Ltd.";
}).call(this);

