/**
 * Simple Tabs - jQuery plugin
 *
 * @author Aurimas Tubis a.k.a Okanakis <aurimas@kryptis.lt>
 *
 * Built for jQuery library
 * http://jquery.com
 */

(function($) {
	$.fn.simpleTabs = function(settings){
		// default|available configuration properties
		var defaults = {
            tabId:'',
            activateParent:false,
            initActive:false,
            initFirst:false,
            initLast:false,
            callBack:false
        };
        
		var settings = $.extend(defaults, settings);  
        
        var currentObj = $(this);
		
        if(settings.initFirst) {
            currentObj.find('li:first-child').addClass('first');
        }
        if(settings.initLast) {
            currentObj.find('li:last-child').addClass('last');
        }
        
        currentObj.find('li a').click(function(){
            var parentOuter = $(this).parent().parent();
            var tabId = $(this).attr('href');
            var relation = $(this).attr('rel');
            
            parentOuter.find('.active').removeClass('active');
            if(settings.activateParent)
            {
                $(this).parent().addClass('active');
            }
            else
            {
                $(this).addClass('active');
            }
            
            /*
            if(tabId)
            {
                window.location.hash = tabId;
            }
            */
            
            if(tabId && relation)
            {
                $('#' + relation + ' .tab_content').removeClass('active').hide();
                $('#' + relation + ' ' + tabId).addClass('active').show();
                //$('#' + relation + ' .tab_content').hide();
                //$('#' + relation + ' ' + tabId).show();
            }
            
            //$.cookie('page-tab-view-{/literal}{$item.id}{literal}', tabId, { expires: 30 });
            
            if(settings.callBack)
            {
                settings.callBack($(this));
            }
            
            $(this).blur();
            return false;
        });
        
        function _initTab() {
            var hash = window.location.hash;
            
            if(hash)
            {
                settings.tabId = hash;
            }
            else if(settings.initActive)
            {
                currentObj.find('li:first-child a').trigger('click');
                return;
            }
            
            if(!settings.tabId)
            {
                return;
            }
            
            var tabId = settings.tabId; //$.cookie('page-tab-view-'.settings.tabId);
            var contentObj = $(tabId);
            var linkObj = $('.tabs_nav li a[href$=\\'+tabId+']');
            
            if (contentObj.length && linkObj.length)
            {
                linkObj.trigger('click');
            }
        }
        
        _initTab();
	};

})(jQuery);

