$(document).ready(function() {

    $("#carousel").cycle({
        fx: 'fade',
        timeout: 7000,
        next: '#carouselnav .next',
        prev: '#carouselnav .previous',
        pause: true,
        pauseOnPagerHover: true
    })

    $("input#email").focus(function() {
        if ($(this).val() == "your email address") {
            $(this).val('');
        }
    }).blur(function() {
        if ($(this).val() == "") {
            $(this).val('your email address');
        }
    });

    $(function() {
        var index = 0,
            hash = window.location.hash;
        if (hash) {
            index = /\d+/.exec(hash)[0];
            index = (parseInt(index) || 1); // slides are zero-based
        }
        $('#menusbody').cycle({
            fx: 'scrollHorz',
            startingSlide: index,
            speed: 400,
            pager: "#menupager",
            cleartypeNoBg: true,
            pagerAnchorBuilder: function(idx, slide) {
                return '<h1 class="cufon smallunderline ' + idx + '" style="cursor: pointer">' + slide.title + '</h1>';
            },
            pagerClick: function() {
                setTimeout(cufonrefresh, 120);
            },
            after: function(curr, next, opts) {
                if (opts.currSlide != 0) {
                    window.location.hash = opts.currSlide + next.title.replace(/ /g, "_");
                    var $ht = ($(this).outerHeight("true")) * 1.12;
                    $(this).parent().animate({
                        height: $ht
                    });
                }
            }
        });
        $("#menusbody").cycle("pause");
    });


    $('.reviewbody').jScrollPane({
        scrollbarWidth: 6
    });

    $("#locationcarousel").cycle({
        fx: 'fade',
        speed: 500,
        timeout: 5500,
        pager: '#locationcarouselnav',
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#">' + idx + '</a></li>';
        }
    });

    /* foursquare section */
    if ($("#foursquare").length) {


        var jsonurl = "https://api.foursquare.com/v2/venues/" + $("#foursquare").attr("title") + "?client_id=BGEAJWWBVW4GDCDO1O1CDEI3XNJXTWEA3ZN5XBJ4L0RWSKST&client_secret=TRZZ0HVW3GTKALNFCWHB3L4MCJUASDESEEJ1WSBO5M5GUJMW&callback=?";


        $.getJSON(jsonurl, function(data) {
            $("#foursquare").removeClass("loading");
            var tips = new Array();
            for (var i in data.response.venue.tips.groups[0].items) {
                tips[i] = data.response.venue.tips.groups[0].items[i].text;
            }

            var content = "";
            content += '<div class="herenow">Here Now <span>' + data.response.venue.hereNow.count + '</span></div>';
            content += '<div class="totalcheckins">Total Checkins <span>' + data.response.venue.stats.checkinsCount + '</span></p>';
            content += '<div class="mayor">Mayor: <span style="line-height: 30px; color: #FFF;"><img src="' + data.response.venue.mayor.user.photo + '" height="30" align="left" />&nbsp;' + data.response.venue.mayor.user.firstName + ' ' + data.response.venue.mayor.user.lastName + '</span></div>';
            content += '<iframe src="http://foursquare.com/button.html?vid=' + $("#foursquare").attr("title") + '&color=blue" allowtransparency="true" frameborder="0" scrolling="no" style="width:165px; height:25px;"></iframe>'

            content += '<h2 class="tips">Tips</h2><ul id="tips">';
            $.each(tips, function(index, value) {
            	if(value.search("d-bag") == -1 && value.search("Worst") == -1) {
	                content += '<li>&#8220;' + value + '&#8221;</li>';
                }
            });
            content += "</ul>";
            content += '<div class="foursqdisclaimer">This is a live feed. We are not responsible for poor grammar or bad taste.</div>';            
            if (window.console) console.log(data);
            $("#foursquare").html(content);
            $("#tips").cycle({
                timeout: 5000
            });
        });

    } /* end foursquare */


    /* flickr gallery */

    if ($("#flickr").length) {

        /* standard public http://api.flickr.com/services/feeds/groups_pool.gne?id=1456996@N22&lang=en-us&format=json&jsoncallback=? */
        /* API: http://api.flickr.com/services/rest/?&method=flickr.groups.pools.getPhotos&group_id=1456996@N22&api_key=8bad1d9388f797bc15c6597320ee98d6&format=json&jsoncallback=? */

        $.getJSON("http://api.flickr.com/services/rest/?&method=flickr.groups.pools.getPhotos&group_id=1456996@N22&api_key=8bad1d9388f797bc15c6597320ee98d6&format=json&extras=url_sq,url_t,url_s,url_m,url_z,url_l,url_o,path_alias&jsoncallback=?", function(data) {
            if (window.console) console.log(data);
            $("#flickr").removeClass("loading");
            $.each(data.photos.photo, function(i, item) {
                $('<img style="width:75px; height: 75px" />').attr("src", item.url_sq).appendTo("#flickr").data(item).wrap('<div class="flickrimage"></div>')
                .click(function() {
                	if($(this).parent().hasClass("opened")) {
                	 	window.open("http://www.flickr.com/photos/" + $(this).data().owner + "/" + $(this).data().id + "/in/pool-1456996@N22/", "_blank");
                	 } else {
		                   
		                var newheight = $(this).data().height_m * (257 / $(this).data().width_m);
	                	var parentheight = (91*(Math.round(newheight/91))) + 75;
		                if($(this).data().title.length > 48) {
						parentheight += 91;
		                }
		                    $(".opened img").animate({
		                        width: "75px",
		                        height: "75px"
		                    }, 350);
		                    $(".opened h1").remove();
		                    $(".opened p").remove();                    
		                    $(".opened").animate({
		                        width: "75px",
		                        height: "75px"
		                    }, 350).removeClass("opened");
		                    $(this).attr("src", $(this).data().url_m)
		                    .animate({
		                        width: "257px",
		                        height: newheight
		                    }, 350)	
		                    	.parent().addClass("opened").animate({width:"257px", height: parentheight}, 350)
		                    	.append('<h1 class="cufon">' + $(this).data().title + '</h1>')
		                    	.append('<p>By ' + $(this).data().ownername + "</p>");
		                    cufonrefresh();
		                 }
                });
            });
        });


    }

    /* end flickr */
});








function cufonrefresh() {
    Cufon.replace('h1.cufon');
}

function is_int (mixed_var) {
    if (typeof mixed_var !== 'number') {
        return false;
    }

    return !(mixed_var % 1);
}

