// $Id

/* 
 * Réseau wallon de Développement rural
 * Library functions
 * Last modified: 22-04-2010
 * Author: Aki Karkkainen
 */

var pwdr = {

    //shared properties used by all the methods below


    //generic init call 
    init : function(){
        // On all pages
        pwdr.labelToValue();
        pwdr.openNewWindow();
        pwdr.toggleMenu();
        pwdr.checkCurrentPage();
        pwdr.slideNav();
	},

    //page specific init calls
    pageWhosWho : {
        init : function(){
            // Who's Who page specific jQuery functions
            pwdr.toggleSections();
            pwdr.labelToValue();
            pwdr.openNewWindow();
            pwdr.toggleMenu();
            pwdr.checkCurrentPage();
            pwdr.slideNav();
		}
    },
    pageGAL : {
        init : function(){
            // GAL page specific jQuery functions
            pwdr.toggleGAL();
            pwdr.labelToValue();
            pwdr.openNewWindow();
            pwdr.toggleMenu();
            pwdr.checkCurrentPage();
            pwdr.slideNav();
		}
    },

    //methods
    toggleSections : function(){
        var paragraphs = jQuery('.whoswho #content div');
        //	paragraphs.hide().filter(':first').show();
        jQuery('.whoswho #content ul a.inside').click(function () {
            paragraphs.hide();
            paragraphs.filter(this.hash).fadeIn('fast');
            return false;
        }).filter(':first').click();
    },

    toggleGAL : function(){
        var areas = jQuery(".gal #content div[id*='gal']");
        //	paragraphs.hide().filter(':first').show();
        jQuery('.gal #content #nav-gal a').click(function () {
            areas.hide();
            areas.filter(this.hash).fadeIn('normal');
            return false;
        }).filter(':first').click();
    },

    openNewWindow : function(){
        jQuery('a[rel="external"]').attr('target', '_blank');
    },

    labelToValue : function(){
        // CSS class names
        // put any class name you want
        // define this in external css
        var inactive = "inactive";
        var active = "active";
        var focused = "focused";

        // function
        jQuery("#loginform label").each(function(){
            obj = document.getElementById(jQuery(this).attr("for"));
            if((jQuery(obj).attr("type") == "text") || (jQuery(obj).attr("type") == "password") || (obj.tagName.toLowerCase() == "textarea")){
                jQuery(obj).addClass(inactive);
                var text = jQuery(this).text();
                jQuery(this).css("display","none");
                jQuery(obj).val(text);
                jQuery(obj).focus(function(){
                    jQuery(this).addClass(focused);
                    jQuery(this).removeClass(inactive);
                    jQuery(this).removeClass(active);
                    if(jQuery(this).val() == text) {
						jQuery(this).val("");
					}
                });
                jQuery(obj).blur(function(){
                    jQuery(this).removeClass(focused);
                    if(jQuery(this).val() == "") {
                        jQuery(this).val(text);
                        jQuery(this).addClass(inactive);
                    } else {
                        jQuery(this).addClass(active);
                    }
                });
            }
        });
    },
	
	toggleMenu : function(){
		//Add a class to indicate JS support
		//Use this class with CSS to overwrite suckerfish in JS enhanced version
		jQuery('#main ul.navEND').addClass('dyn');

		//If the the clicked item has children, make that submenu visible and return false
		//For all other links, return true
		jQuery('#leftcol ul.navEND > li a').click(function() {
			if ( jQuery(this).parent('li').find('ul').length ) {
				jQuery(this).parent('li').children('ul.SubNav').addClass('show');
				return false;
			}
            else {
                return true;
            }
		});
	},

    //Check URL and if the query string 'sec_id' exists (submenu link has been clicked), then keep submenu open
    checkCurrentPage : function() {
        var args = pwdr.getQueryStringArgs();
        if (args.sec_id) {
			jQuery('#leftcol ul.naENDv ul.SubNav').addClass('show');
        }
    },

    //This comes from Nicholas Zakas (http://www.nczonline.net/) book:
    //"Professional JavaScript for Web Developers", Wrox
    getQueryStringArgs : function () {
        //get query string without the initial ?
        var qs = (location.search.length > 0 ? location.search.substring(1) : "");
        //object to hold data
        var args = {};

        //get individual items
        var items = qs.split(" & ");
        var item = null,
        name = null,
        value = null;
        //assign each item onto the args object
        for (var i=0; i < items.length; i++){
            item = items[i].split("=");
            name = decodeURIComponent(item[0]);
            value = decodeURIComponent(item[1]);
            args[name] = value;
        }
        return args;
    },

    //Hide all submenus initially, and show the one that is under the clicked item
    //Cancel default link behaviour for items that have submenus (just expand it)
    //For all other links, return true
    slideNav : function() {
        jQuery('.navEND li ul').hide();
        jQuery('.navEND li > a').click(function() {
            var $childMenu = jQuery(this).parent('li').find('ul');
            if ($childMenu.length) {
                $childMenu.slideDown('fast');
                return false;
            }
            else {
                return true;
            }
        });
    }

}; //END pwdr object
