function PageLocator(propertyToUse, dividingCharacter) {
    this.propertyToUse = propertyToUse;
    this.defaultQS = 1;
    this.dividingCharacter = dividingCharacter;
}
PageLocator.prototype.getLocation = function() {
    return eval(this.propertyToUse);
}
PageLocator.prototype.getHash = function() {
    var url = this.getLocation();
    if (url.indexOf(this.dividingCharacter) > -1) {
        var url_elements = url.split(this.dividingCharacter);
        return url_elements;
    } else {
        return this.defaultQS;
    }
}
PageLocator.prototype.getHref = function() {
    var url = this.getLocation();
    var url_elements = url.split(this.dividingCharacter);
    return url_elements[0];
}
PageLocator.prototype.makeNewLocation = function(new_qs) {
    return this.getHref() + this.dividingCharacter + new_qs;
}


var MyFunctions = function() {

    var j = -1;

    jQuery.fn.stripTags = function() { return this.replaceWith(this.html().replace(/<\/?[^>]+>/gi, '')); };

    return {

        getHashtags: function(chr) {

            var locator = new PageLocator("window.location.href", chr);
            return locator.getHash();

        },

        getNewWidth: function(origWidth, origHeight, newheight) {
            var ratio = origWidth / origHeight;
            return newheight * ratio;
        },

        getImg: function(i) {
            $("#theshow").show();
            $("#theshow").attr("src", "/_css/loading.gif");
            $("#theshow").attr("src", imgs[i]);
            $("#thedesc").html(descs[i]);
            $("#thelink").html(links[i]);

            // INDICA A FOTO CLICADA

            MyFunctions.setPhotoLinkHighlight(i);

            // AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
            jQuery(function($) {
                $("a[rel^='lightbox']").slimbox({/* Put custom options here */
            }, null, function(el) {
                return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
            });
        });
    },

    getPhoto: function(url,i) {
            $("#theshow").show();
            $("#theshow").attr("src", "/_css/loading.gif");
            $("#theshow").attr("src", (url));
            $("#thedesc").html('');
            $("#thelink").html('');

            // INDICA A FOTO CLICADA

            MyFunctions.setPhotoLinkHighlight(i);    
	
	},
    
    getWork: function(tags, size, title) {

        var media;
        var totalWidth = 0;

        j = -1;

        imgs = [];
        descs = [];
        links = [];
        $("#selectors").html('');
        $("#selectors").show();
        $("#theshow").hide();
        $("#thenewshow").hide();
        $("#thedesc").html('');
        $("#thelink").html('');
        $("#therest").html('');

        $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=" + tags + "&tagmode=all&id=23166372@N05&lang=en-us&format=json&jsoncallback=?", function(data) {

            // TITULO

            //$('#selectors').append('<b>' + title + '</b><br />');


            $.each(data.items, function(i, item) {
                //var h = $(window).height();

                switch (size) {

                    case "big":
                        media = item.media.m.replace('_m', '_b');
                        break;
                    case "small":
                        media = item.media.m.replace('_m', '_s');
                        break;
                    case "medium":
                        media = item.media.m;
                        break;
                    case "normal":
                        media = item.media.m.replace('_m', '');
                        break;
                    default:
                        media = item.media.m.replace('_m', '');
                        break;

                }


                imgs[i] = media;
                descs[i] = "<b>" + String(item.title) + "</b><br /><em>" + String(item.date_taken).substring(0, 10) + "</em> ";
                links[i] = "<a href=\"" + item.media.m.replace('_m', '_b') + "\" rel=\"lightbox\" title=\"" + String(item.title) + " &lt;em&gt;" + String(item.date_taken).substring(0, 10) + "&lt;/em&gt; &lt;a class='black' href='" + item.link + "'&gt;open in Flickr&lt;/a&gt; \">open bigger over black </a>";


                var hashtags = MyFunctions.getHashtags('?');
                $('#selectors').append('<a href="#" id="photolink' + i + '"><img src="' + item.media.m.replace('_m', '_s') + '" alt="' + item.title + '"></a>');
                $('#photolink' + i).click(function() { MyFunctions.getImg(i); });


            });

            // AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
            jQuery(function($) {
                $("a[rel^='lightbox']").slimbox({/* Put custom options here */
            }, null, function(el) {
                return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
            });


            // SE TIVER HASHTAGS, SACA A PHOTO!!!
            var hashtags = MyFunctions.getHashtags('#');
            if (hashtags.length > 1 && hashtags[1].length > 0) MyFunctions.getImg(hashtags[1]);
            $("#menu").fadeIn(1000);

        });

    });

},


getContentFromHashtags: function() {
    var hashtags = MyFunctions.getHashtags('?');
    var cat = hashtags[1].substr(0, hashtags[1].indexOf('#'))
    $("#" + cat).click();

},


setMenuLinkHighlight: function(i) {

    $(".selected").removeClass("selected");
    $(i).addClass("selected");

},

setPhotoLinkHighlight: function(i) {

    $("#selectors a").removeClass("selected");
    $("#photolink" + i).addClass("selected");

},

resetScene: function() {
    $("#selectors").hide();
    $("#selectors").html('');
    $("#theshow").hide();
    $("#thedesc").html('');
    $("#thelink").html('');
    $("#therest").html('');
},

init: function() {

	// SETS SCENERY TO BLACK MOODS
	
        $("#logoimage").attr("src", "/_images/logo_white.png");
        $("body").css("background-color", "#000");
        $("body").css("color", "#fff");
        $("#menu").css("background-color", "#000");
        $("#menu a").css("color", "#fff");
        $("#selectors a").css("color", "#fff");


    $("#bandw").click(function() {
        MyFunctions.getWork("website,black and white", "", "Black and White");
        MyFunctions.setMenuLinkHighlight("#bandw");
    });

    $("#autumn").click(function() {
        MyFunctions.getWork("website,autumn", "", "Autumn");
        MyFunctions.setMenuLinkHighlight("#autumn");
    });

    $("#spring").click(function() {
        MyFunctions.getWork("website,spring", "", "Spring");
        MyFunctions.setMenuLinkHighlight("#spring");
    });

    $("#sunsets").click(function() {
        MyFunctions.getWork("website,sunsets", "", "Sunsets");
        MyFunctions.setMenuLinkHighlight("#sunsets");
    });

    $("#landscapes").click(function() {
        MyFunctions.getWork("website,landscape", "", "Landscapes");
        MyFunctions.setMenuLinkHighlight("#landscapes");

    });

    $("#nature").click(function() {
        MyFunctions.getWork("website,nature", "", "Nature");
        MyFunctions.setMenuLinkHighlight("#nature");

    });

    $("#weddings").click(function() {

        MyFunctions.getWork("website,wedding", "", "Weddings");
        MyFunctions.setMenuLinkHighlight("#weddings");

    });
    $("#isaeandre").click(function() {

        MyFunctions.getWork("website,isabel e andre", "", "Isabel e André");
        MyFunctions.setMenuLinkHighlight("#isaeandre");

    });


    $("#rbar").click(function() {

        MyFunctions.getWork("website,rbar", "", "Red Bull Air Race - Porto");
        MyFunctions.setMenuLinkHighlight("#rbar");

    });

    $("#cinema").click(function() {
        MyFunctions.getWork("website,cinematic", "", "Cinematic photography");
        MyFunctions.setMenuLinkHighlight("#cinema");

    });

    $("#photoshoped").click(function() {
        MyFunctions.getWork("website,photoshoped", "", "Photoshoped photography");
        MyFunctions.setMenuLinkHighlight("photoshopedcinema");

    });

    $("#6am").click(function() {
        MyFunctions.getWork("website,6am photography", "", "6am photography");
        MyFunctions.setMenuLinkHighlight("#6am");

    });

    $("#stock").click(function() {
        MyFunctions.setMenuLinkHighlight("#stock", "", "Stock photography");
        MyFunctions.setMenuLinkHighlight(1);

    });

    $("#tilt-shift").click(function() {
        $("#spinner").show();
        $("#images").empty();
        MyFunctions.getWork("website,tilt-shift", "", "Tilt-shift photography");
        MyFunctions.setMenuLinkHighlight("#tilt-shift");

    });

    $("#lensbaby").click(function() {
        MyFunctions.getWork("website,lensbaby", "", "Lensbaby photography");
        MyFunctions.setMenuLinkHighlight("#lensbaby");

    });

    $("#iphone").click(function() {
        MyFunctions.getWork("website,best camera", "", "iPhone photography");
        MyFunctions.setMenuLinkHighlight("#iphone");
    });


    $("#timelapses").click(function() {
        MyFunctions.getMovies();
        MyFunctions.setMenuLinkHighlight("#timelapses");
    });

    $("#vision").click(function() {
       MyFunctions.resetScene();
        $("#therest").load("/vision.html", "", function() { $("#spinner").hide(); });
        MyFunctions.setMenuLinkHighlight("#vision");

    });
    $("#about").click(function() {
       MyFunctions.resetScene();
        $("#therest").load("/about.html", "", function() { $("#spinner").hide(); });
        MyFunctions.setMenuLinkHighlight("#about");

    });

    $("#contact").click(function() {
       MyFunctions.resetScene();
        $("#therest").load("/contact.html", "", function() { $("#spinner").hide(); });
        MyFunctions.setMenuLinkHighlight("#contact");

    });

    $("#zoo").click(function() {
       MyFunctions.resetScene();
        $("#therest").load("/adayinthezoo/index.html", "", function() { $("#spinner").hide(); });
        MyFunctions.setMenuLinkHighlight("#contact");

    });



    $(".selectBlack").click(function() {
        $("#logoimage").attr("src", "/_images/logo_white.png");
        $("body").css("background-color", "#000");
        $("body").css("color", "#fff");
        $("#menu").css("background-color", "#000");
        $("#menu a").css("color", "#fff");
        $("#selectors a").css("color", "#fff");

    });

    $(".selectWhite").click(function() {
        $("#logoimage").attr("src", "/_images/logo.png");
        $("body").css("background-color", "#fff");
        $("body").css("color", "#333");
        $("#menu").css("background-color", "#fff");
        $("#menu a").css("color", "#333");
        $("#selectors a").css("color", "#333");
    });


}
};
} ();
