/**
 * @author Kristofer Forsell
 */

// ensure that the debug console exists
if (window.loadFirebugConsole) {
    window.loadFirebugConsole();
} else if (!window.console) {
    window.console = {
        info: function (s) {},
        log: function (s) {},
        warn: function (s) {},
        error: function (s) {}
    };
}

function CoreClass() {
    this.me = "core";
    var templateUrl;
}

CoreClass.prototype = {
    init: function () {

        //Global URL to the template
        templateUrl = $('#template-dir').val();

        // initialize
        this.initAddress();
        this.initNav();
		
		//change to sans-serif if on windows machine
		if (navigator.appVersion.indexOf("Win")!=-1){
			$('body').addClass('windows');
		}
		

    },
    initAddress: function () {

        // Address handler
        $.address.init(function (event) {

        }).change(function (event) {

            // Close all views
            blom.closeViews();
			
            if (event.value.split("/")[1] === "news") {
				blom.news.loadNews( $('.news').find('a').attr('href') );
				log('Load News');
            } else if (event.value.split("/")[1] === "diary") {
                blom.diary.loadDiary( $('.diary').find('a').attr('href') );
				log('Load Diary');
            } else if (event.value.split("/")[1] === "clients") {
				log('Load Clients');
                blom.clients.loadClients( $('.clients').find('a').attr('href') );
            } else if (event.value.split("/")[1] === "contact") {
                log('Load Contacts');
                blom.contact.loadContact( $('.contact').find('a').attr('href') );
            } else if(event.value.split("/")[1] === "gallery"){
          
				blom.closeViews();
            	blom.gallery.currentGallery  = event.value.split("/")[2];
				
				if( event.value.split("/")[3] !== undefined ){
					var orderId = event.value.split("/")[3];
				} else {
					var orderId = 0;
				}
				
            	blom.gallery.loadGallery( './../photo_admin/index.php/xml', orderId );
            		
			} else {
				log('Load Home');				
                blom.home.loadThumbs();
				blom.home.isOpen = true;
            }

        }).history(true);

    },

    closeViews: function () {
        blom.home.closeView();
        blom.news.closeView();
        blom.diary.closeView();
        blom.clients.closeView();
        blom.contact.closeView();
        blom.gallery.closeView();
		blom.lightTable.closeView();
    },

    initNav: function () {
       
		$('#navigation').delay(1300).fadeIn(500).find('.accordion li ul').hide();
		blom.hideSubnav();
        // Bind Mouse Down
        $('#navigation .accordion').find("li").each(function () {
            $(this).not("showAll").bind("click", function () {
				
				if( blom.home.isOpen === false ){
					// log('Home is not open');
					blom.closeViews();
					blom.home.openView();
				}
				
                if ($(this).hasClass("active") === false && $(this).parent().parent().hasClass("active") === false) {
                    blom.hideSubnav();
                    $(this).find("ul").slideDown(150);
                    $(this).addClass("active");
                } 
				
				blom.lightTable.closeView();
			});
        });
        //
        $('#navigation .accordion').find("li.filter").each(function () {
			
            $(this).bind("click", function () {
               // log('clicked', $(this).get(0));
				if( $(this).parent().hasClass('sub-menu')){
					// $(this).addClass('activeGallery');
				}
				
 				blom.home.filterItems($(this).attr("class"));
				return false;
            });
        });

        // Build List
        var topIndex = 1;
        $('#navigation .accordion').find("ul.sub-menu").each(function () {
            var caseIndex = 1;
            $(this).find("li").each(function () {
                $(this).prepend("<span class='legalIndex' >0" + topIndex + ".0" + caseIndex + "</span>");
                caseIndex++;
            });
            topIndex++;
        });

        // Show All
        $('#navigation .showAll, #blom-logo').live("click", function () {
				log('Show all')
				blom.hideSubnav();
				blom.closeViews();
				$.address.value('');
				blom.home.loadThumbs();
				return false;
        });

		// Light table
		$('#navigation .light-table').bind("click", function(){
			blom.hideSubnav();
			
			if( blom.home.isOpen == false ){
				blom.closeViews();
				blom.home.openView();
			}
			
			blom.lightTable.openView();
		});

		// For the other nav items
		$('#menu-daniel-blom-2011-main').find('li').live('click', blom.regularNavClick );

    },

	regularNavClick: function() {
		
		if( $(this).hasClass('news') === true ){
			$.address.value('news');
		} else if( $(this).hasClass('diary') === true ){
			$.address.value('diary');
		} else if( $(this).hasClass('clients') === true ){
			$.address.value('clients');
		} else if( $(this).hasClass('contact') === true ){
			$.address.value('contact');
		}
			blom.hideSubnav();
			return false;
	},

    hideSubnav: function () {
        $('#navigation .accordion').find("li > ul").each(function () {
            $(this).parent().removeClass("active");
            $(this).slideUp(150);
        });
    },

    initClass: function (className) {
        if (typeof blom[className].init === "function") {
            this[className].init();
        }
    },
	
	changeIndexTitle: function( str ){
		$('#navigation .showAll').fadeOut(100,function(){ $(this).text(str).fadeIn(100); });
	},
	
    toString: function () {
        return this.me;
    }

};

blom = new CoreClass();

$(document).ready(function () {
    blom.init();
});
