var mobile = DetectMobileLonger();
if(mobile) window.location = '/mobile/';

var aesthetic =["images/gallery/photos/fullsize/070309_003.jpg","images/gallery/photos/fullsize/070309_006.jpg","images/gallery/photos/fullsize/072009_003.jpg","images/gallery/photos/fullsize/072009_004.jpg","images/gallery/photos/fullsize/072009_023.jpg","images/gallery/photos/fullsize/072009_044.jpg","images/gallery/photos/fullsize/070309_016.jpg","images/gallery/photos/fullsize/072009_025.jpg","images/gallery/photos/fullsize/070309_013.jpg","images/gallery/photos/fullsize/070309_022.jpg","images/gallery/photos/fullsize/072009_011.jpg","images/gallery/photos/fullsize/070309_024.jpg","images/gallery/photos/fullsize/070309_029.jpg","images/gallery/photos/fullsize/070309_030.jpg","images/gallery/photos/fullsize/072009_029.jpg","images/gallery/photos/fullsize/072009_035.jpg","images/gallery/photos/fullsize/072009_031.jpg","images/gallery/photos/fullsize/072009_038.jpg","images/gallery/photos/fullsize/072009_040.jpg","images/gallery/photos/fullsize/070309_031.jpg","images/gallery/photos/fullsize/072009_001.jpg","images/gallery/photos/fullsize/072009_015.jpg","images/gallery/photos/fullsize/070309_007.jpg","images/gallery/photos/fullsize/072009_020.jpg","images/gallery/photos/fullsize/072009_057.jpg","images/gallery/photos/fullsize/L1000029.jpg","images/gallery/photos/fullsize/070309_001.jpg"];

var food = ["images/gallery/photos/fullsize/070309_013.jpg","images/gallery/photos/fullsize/070309_022.jpg","images/gallery/photos/fullsize/070309_024.jpg","images/gallery/photos/fullsize/070309_029.jpg","images/gallery/photos/fullsize/070309_030.jpg","images/gallery/photos/fullsize/072009_029.jpg","images/gallery/photos/fullsize/072009_035.jpg","images/gallery/photos/fullsize/072009_031.jpg","images/gallery/photos/fullsize/072009_038.jpg","images/gallery/photos/fullsize/072009_040.jpg","images/gallery/photos/fullsize/070309_031.jpg","images/gallery/photos/fullsize/072009_001.jpg","images/gallery/photos/fullsize/072009_015.jpg"];

var restaurant = ["images/gallery/photos/fullsize/070309_003.jpg","images/gallery/photos/fullsize/070309_006.jpg","images/gallery/photos/fullsize/072009_003.jpg","images/gallery/photos/fullsize/072009_004.jpg","images/gallery/photos/fullsize/072009_023.jpg","images/gallery/photos/fullsize/072009_044.jpg","images/gallery/photos/fullsize/070309_016.jpg","images/gallery/photos/fullsize/072009_025.jpg","images/gallery/photos/fullsize/072009_011.jpg","images/gallery/photos/fullsize/070309_007.jpg","images/gallery/photos/fullsize/072009_020.jpg","images/gallery/photos/fullsize/072009_057.jpg","images/gallery/photos/fullsize/L1000029.jpg","images/gallery/photos/fullsize/070309_001.jpg"];

Array.prototype.clone = function () {
	var arr1 = new Array(); 
	for (var property in this) {
		arr1[property] = typeof (this[property]) == 'object' ? this[property].clone() : this[property]
	}
	return arr1;
}

var map;
var gdir;
var geocoder = null;
var addressMarker;
var startAdd = null;
var endAdd = null;
function initialize() {
    map = new GMap2(document.getElementById("gmap"));
    gdir = new GDirections(map, document.getElementById("directions"));
    map.setCenter(new GLatLng(34, 0), 13);
    map.enableScrollWheelZoom();
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
    geocoder = new GClientGeocoder();
    showLocation("8 E 1st St Hinsdale, IL 60521");
}
function onGDirectionsLoad() {}
function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) { //alert("Sorry, we were unable to geocode that address");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker); ////////********///////
        endAdd = place.address; ///////////////////////
        marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
    }
} // showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(address) {
    geocoder.getLocations(address, addAddressToMap);
}
function setStart(response) {
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } else {
        place = response.Placemark[0];
        startAdd = place.address; // + ', ' + place.AddressDetails.Country.CountryNameCode;
        showDir();
    }
}
function setDirections(fromAddress, toAddress, locale) {
    geocoder.getLocations(fromAddress, setStart);
}
function showDir() {
    if (endAdd != null && startAdd != null) {
        $("#directions").empty();
        gdir.load("from: " + startAdd + " to: " + endAdd, {
            "locale": "en_US"
        });
    }
}
function handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code); //   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
    //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_KEY) alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    else alert("An unknown error occurred.");
}
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];
        }
    }
}
var IE6 = false;
$(function () {
	if ($.browser.msie && parseInt($.browser.version.substr(0, 1)) < 7) IE6 = true;
    MM_preloadImages('images/general/bg_w.gif', 'images/general/arrow-left.gif', 'images/general/arrow-right.gif', 'images/general/dark-pointer.gif', 'images/general/pause.gif', 'images/general/play.gif', 'images/general/stop.gif'); // Nav Links
    $('a.new-window').click(function () {
        window.open(this.href);
        return false;
    });
    $("img.semi").css({
        opacity: .5,
        cursor: 'default'
    });
    $(".hovers img").not(".paused,.opaque,.semi").css("opacity", .5).hover(function () {
        $(this).css("opacity", 1);
        var src = $(this).attr("src");
        src = src.replace(/_w/g, '');
        $(this).attr("src", src);
    },
    function () {
        if (!$(this).hasClass("paused") && !$(this).hasClass("opaque")) {
            $(this).css("opacity", .5);
            var src = $(this).attr("src");
            if (src.indexOf('_w') == -1) {
                src = src.replace(/.gif/, '_w.gif');
                $(this).attr("src", src);
            }
        }
    });
    $("#pause").toggle(function () {
        $('#slideshow').cycle('pause');
        $(this).css("opacity", 1).addClass("paused");
    },
    function () {
        $('#slideshow').cycle('resume');
        $(this).css("opacity", .5).removeClass("paused");
    });
    $(".footer").removeClass("footer").addClass("footer"); // IE7 fix
    // nifty player
    $("#stopMusic").click(function () {
        if ($(this).hasClass("stopped")) return;
        $(this).addClass("opaque stopped");
        $("#playMusic,#pauseMusic").removeClass("playing paused opaque").trigger("mouseout");
        niftyplayer('niftyPlayer1').stop();
    });
    $("#playMusic").click(function () {
        if ($(this).hasClass("playing")) return;
        niftyplayer('niftyPlayer1').play();
        $(this).addClass("opaque");
        $("#stopMusic").removeClass("stopped opaque").trigger("mouseout");
    });
    $("#pauseMusic").click(function () {
        if ($("#stopMusic").hasClass("stopped")) return;
        niftyplayer('niftyPlayer1').playToggle();
        var st = niftyplayer('niftyPlayer1').getState();
        if (st == 'paused') $(this).addClass("opaque");
        else $(this).css("opacity", .5).removeClass("opaque");
    });
});
var currentGal = 'aesthetic';
var gal = null;

function gallery(g) {
	currentGal = g;
	var g = eval(g);
    gal = g.clone();
	var ml = -(W/2) + 'px';
	var mt = -((W*(742/960))/2) + "px";
    $(".paused").trigger("click"); // removed "paused" state!
	$('#slideshow').empty().append('<img style="left:50%;top:50%;margin-left:'+ml+';margin-top:'+mt+';" class="bg" src="' + gal.pop() + '" /><img  style="left:50%;top:50%;margin-left:'+ml+';margin-top:'+mt+';" class="bg" src="' + gal.shift() + '" /><img style="left:50%;top:50%;margin-left:'+ml+';margin-top:'+mt+';" class="bg" src="' + gal.shift() + '" />').cycle({
        fx: 'custom',
		startingSlide: 0,
        speed: 10,
        delay: 5000,
        timeout: 10000, // what about a 20 sec delay !
		cssBefore: {left:"50%",top:"50%",marginLeft:ml, marginTop:mt,zIndex: 4,display:"block",opacity:0},
		animIn: {opacity: 1},
		animOut: {opacity: 0},
		cssAfter: {zIndex: 0, display:'none'},
        prev: '#prev',
        // selector for element to use as click trigger for previous slide
        next: '#next',
        // selector for element to use as click trigger for next slide
        nowrap: 0,
        fit: 0,
        autostop: 0,
        before: onBefore
    });
	$("#next").trigger("click");
	var totalSlideCount = 1 + gal.length;
    function onBefore(curr, next, opts, fwd) {
        if (!opts.addSlide) return;
        if (opts.slideCount == totalSlideCount) return; // shift or pop from our slide array
        var nextSlideSrc = fwd ? gal.shift() : gal.pop(); // add our next slide 
        opts.addSlide('<img class="bg" src="' + nextSlideSrc + '" />', fwd == false);
    }
}
var H = null; var W = null;
function scrollbarWidth(){
	var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}
function fixH() {
    H = $(window).height();
    W = $(window).width();
    $(".stripViewer .panelContainer .panel, .stripViewer").height(H - 180);
    jQuery("div.hauto").height(0);
	var sw = scrollbarWidth();
    $("#slideshow").height(H).width(W);
    $("#slideshow img").width(W+sw).height("100%");
    jQuery("div#slider1").codaSlider();
    setTimeout(function () {
        jQuery("a[name]").remove();
		//var mobile = DetectMobileLonger();
		//if(mobile) $(".stripViewer .panelContainer .panel, .stripViewer").css("background","#000");
    },
    10);
    var l = $(".tab1").offset().left;
    $("#player").css("left", l - 90);
    var l = $(".tab6").offset().left;
    $("#nav").css({
        left: l + 35,
        right: "auto"
    });
    gallery(currentGal);
    jQuery("div#nav").show();
    if (IE6) {
        $(".stripNav, .panel").css("opacity", .7);
    }

	// menu tabs
	
	$("#p7APM_1 h3").each(function(){
		$(this).parent().next().hide();
		$(this).unbind().click(function(){
			jQuery(".stripViewer .panelContainer .panel").css("overflow-y","hidden");
			if($(this).parent().next().css('display')!='none'){$(this).parent().next().hide(); 
			jQuery(".stripViewer .panelContainer .panel").css("overflow-y","auto");
			return;}
			$("#p7APM_1 h3").parent().next().hide();
			$(this).parent().next().show();
			jQuery(".stripViewer .panelContainer .panel").css("overflow-y","auto");
			return (false);
		});
	});
	
}
jQuery(window).load(function () {
    $("#playMusic").trigger("mouseover").addClass("opaque playing");
    fixH();
    $('.bubbleInfo').each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 500;
        var hideDelayTimer = null;
        var beingShown = false;
        var shown = false;
        var trigger = $('.trigger', this);
        var info = $('.popup', this).css('opacity', 1);
        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) { // don't trigger the animation again
                return;
            } else { // reset position of info box
                beingShown = true;
                var w = info.width("auto").outerWidth();
                var l = -(w / 2) + 10;
                info.css({
                    top: -25,
                    width: w,
                    left: l,
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 1
                },
                time, 'swing', function () {
                    beingShown = false;
                    shown = true;
                });
            }
            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                },
                time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });
            },
            hideDelay);
            return false;
        });
    });
    setTimeout(initialize, 2000);
    $("#fnd").click(function () {
        var frm = $("#from").val();
        if (frm != '') {
            setDirections(frm, "8 E 1st St Hinsdale, IL 60521", "en");
        }
    });
    $("td.popup-contents span:not('.sel')").live("click", function () {
        $('#slideshow').cycle('pause').empty();
        $("td.popup-contents span.ch").removeClass("sel");
        $(this).addClass("sel");
        var ga = ($(this).attr("id"));
        gallery(ga);
        $("#stripNav0 a:eq(5)").trigger("click");
    });
    if ($.browser.msie) {
        $("#header").each(function () {
            var bg = $(this).css("backgroundImage");
            if (bg != 'none') {
                bg.match(/^url[("']+(.*\.png)[)"']+$/i);
                bg = RegExp.$1;
                $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg + "', sizingMethod='scale')").css("backgroundImage", "none");
            }
        });
        $("img.pngfix").each(function () {
            $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr('src') + "', sizingMethod='scale')").attr("src", "blank.gif");
        });
    } // music
    setTimeout(function(){$("body").append('<div class="hidden"><object id="niftyPlayer1" type="application/x-shockwave-flash" data="niftyplayer/niftyplayer.swf?file=music/Track03e.mp3&amp;as=1" width="165" height="40"><param name="movie" value="niftyplayer/niftyplayer.swf?file=music/Track03e.mp3&amp;as=1" /></object></div>');
		$("div#player").show();
	}, 5000);
	$(window).resize(function () {
        fixH();
    });
});