/**
 * @author Nexus interacative
 * @version 1.4.3 2011-10-12
 */
(function ($) {
	
	$.fn.bookmark = function () {
		
		this.each(function () {
			
			var THAT = this;
			
			var Bookmark = function () {
				return Bookmark = {
					that: '',
					bookmark: '',
					bookmarkHttp: '',
					bookmarkName: '',
					containers: '',
					
					regexp: /#bookmark\-(.*)/i,
					
					_init: function() {
						var _that = this;
						
						this.that = THAT;
						this.bookmark = this.getBookmark();
						this.bookmarkName = this.getBookmarkName();
						this.containers = this.getContainers();
						this.bookmarkHttp = this.getBookmarkHttp();
						
						this.getActive();
						this.getBookmarkName();
						this.getContainers();
						this.setActive();
						console.log(this.bookmarkName);
						
						$.each(this.bookmark, function( key , value ) {
							$(value).click(function(){
								document.location.href = $(this).find('a').attr('href');
								_that.setActive();
							});
						});
						
						setInterval(function() {_that.setActive();}, 500);
					},
					
					/**
					 * Ustawia aktyaną zakładkę
					 */
					setActive: function() {
						var _that = this;
						$.each(_that.containers, function( key , value ) {
							if ($(value).attr('active') == undefined) {
								$(value).attr('active', 'false');
							}
							if ($(value).attr('id') == _that.getActive()) {
								if ($(value).attr('active') == "false") {
									$(value).show();
									if ($(value).find('iframe').get()[0])
									{
										$(value).find('iframe').get()[0].src = $(value).find('iframe').get()[0].src;
									}
									$(value).attr('active', 'true');
									if (_that.bookmarkHttp[_that.getActive()] != '') {
										$.get(_that.bookmarkHttp[_that.getActive()], function(data) {
											$(value).html(data);
										});
									}
								}
							} else {
								if ($(value).is(':visible')) {
									$(value).hide();
									$(value).attr('active', 'false');
								}
							}
						});	
						
						$.each(_that.bookmark, function( key , value ) {
							if (new String($(value).find('a').attr('href')).search(_that.getActive()) > -1 ) {
								$(value).addClass('active');
							} else {
								$(value).removeClass('active');
							}
						});
						
						
					},
					
					/** 
					 * Zwraca nazwę aktywnej zakładki.
					 */
					getActive: function () {
						var _that = this;
						if (new String(document.location).search(_that.regexp) > -1) {
							
							var _bookmarkName = new RegExp(_that.regexp).exec(new String(document.location))[1];
							
							/**
							 * Sprawdza czy zakładka o takiej nazwie istnieje.
							 */
							for (var i = 0; i < this.bookmarkName.length; i++) {
								if (_bookmarkName == this.bookmarkName[i]) {
									return _bookmarkName;
								}	
							}
							return this.bookmarkName[0];
						} else {
							return this.bookmarkName[0];
						}
					},
					
					/**
					* Zwraca zakładki
					*/
					getBookmark: function () {
						return $(this.that).find('> ul li').get();
					},
					
					/**
					* Zwraca Http zakładki
					*/
					getBookmarkHttp: function () {
						var _that = this;
						var tmp = new Array();
						var searchResult = (document.location.href.search('#bookmark-') > -1) ? document.location.href.search('#bookmark-') : document.location.href.length;
						var href = document.location.href.substr(0, searchResult);
						$(this.that).find('> ul li a').each(function ( key , value ) {
							tmp[_that.bookmarkName[key]] = $(value).attr('href').replace('#bookmark-' + _that.bookmarkName[key], '');
							$(value).attr( 'href' , href + $(value).attr('href').replace( $(value).attr('href'), '#bookmark-' + _that.bookmarkName[key] ) );
						});
						
						return tmp;
					},
					
					/**
					* Zwraca nazwy zakładek
					*/
					getBookmarkName: function () {
						var _bookmarksName = [],
							_that = this;
						
						$(this.that).find('> ul li a').each(function ( key , value ) {
							var _bookmarkName = new RegExp(_that.regexp).exec(new String($(value).attr('href')))[1];
							_bookmarksName.push(_bookmarkName);
						});
						
						return _bookmarksName;
					},
					
					/**
					* Zwraca kontenery zakładek
					*/
					getContainers: function () {
						var _containers = [];
						$.each(this.bookmarkName, function ( key , value ) {
							_containers.push($('#' + value).get());
						});
						
						return _containers;
					}
					
				}
			};
			
			var bookmark = new Bookmark();
			bookmark._init();
			
		});
	};
})(jQuery);

