jQuery.noConflict();

/*Custom accordions*/
jQuery.fn.accordion_custom = function() { 
	
	jQuery(".accordion:not('[class*=disabled]') h2").live('click', function() {
		
		if (jQuery(this).nextAll('div').is(':visible')) {
			//jQuery(this).removeClass('round_bottom');
		} else {
			jQuery(this).removeClass('round_bottom');
		}

		jQuery(this).nextAll('div').slideToggle('normal',function(){
			if (jQuery(this).is(':hidden')) {
				jQuery(this).parent().children('h2').addClass('round_bottom');
				jQuery(this).parent().removeClass('open').addClass('closed');
			} else {
				jQuery(this).parent().children('h2').removeClass('round_bottom');
				jQuery(this).parent().removeClass('closed').addClass('open');
			}
			return false;
		}); 

	});
	
	jQuery('.accordion.grouped').each(function() {	
		jQuery(this).children(".acc_container").each(function() {	
			jQuery(this).children('.accordion:last').children('.acc_container').addClass("last");
		});	
	});
	
	// this replaces switchcontent prototype function
	jQuery(".section").children('div').hide();
	jQuery(".section h3").live('click', function() {
		jQuery(this).nextAll('div').slideToggle('normal',function(){
			if (jQuery(this).is(':hidden')) {
				jQuery(this).parent().children('h3').removeClass('open');
			} else {
				jQuery(this).parent().children('h3').addClass('open');
			}
			return false;
		});
	});

};

/*Pie inject function (for rounded corners in IE)*/
jQuery.fn.pieInject = function(id) {

    c=id.split(",");

	jQuery(c).each(function(int) {

		j=jQuery.trim(c[int]);

		jQuery(j).each(function() {

			class_name = jQuery(this).attr('class');

			if (jQuery(this).is('.acc_container') || jQuery(this).is('.last')){

					if(jQuery(this).parent(".accordion").hasClass("grouped")){
						//alert();
					} else {
						if(!jQuery(this).children("span").hasClass("pie_BL,pie_BR")){
                            jQuery(this).append("<span class='pie_BL'>&nbsp;</span>");
                            jQuery(this).append("<span class='pie_BR'>&nbsp;</span>");
                        }
					}

			} else {
				if(!jQuery(this).children("span").hasClass("pie_TR")){
                    jQuery(this).append("<span class='pie_TR'>&nbsp;</span>");
                }
			}

		});
	});

	/*Sliding tabs*/
	jQuery(".st_tab").each(function() {

        if(!jQuery(this).children("span").hasClass("pie_TR")){
            jQuery(this).append("<span class='pie_TR'>&nbsp;</span>");
        }
	});

	jQuery(".tab_links, .tab_nav, .sitenav_panel ul").each(function() {
        if(!jQuery(this).children("li").children("span").hasClass("pie_TR")){
            jQuery(this).children("li").append("<span class='pie_TR'>&nbsp;</span>");
        }
    });

};

jQuery(document).ready(function(){

/***********************************
	Settings button
*/

	if (jQuery("a#view_settings_btn").length != 0)	{
		jQuery("a#view_settings_btn").fancybox({
			'type'				: 'dropdown',
			'buttonType'		: 'icon',
			'overlayColor'		: '#fff',
			'overlayOpacity'	: 0,
			'autoScale'			: true,
			'showCloseButton'	: false,
			'autoDimensions'	: true,
			'changeSpeed'		: 0,
			'padding'			: 0,
			'speedIn'			: 0,
			'speedOut'			: 0
		});
	}

    /*********************************************
		Show/Hide global search field tip text
	*/
	jQuery("input[type='text']#global_search_field").live('focus', function() {
				jQuery(this).addClass("active");
				if(jQuery(this).attr("value") == "Site search") jQuery(this).attr("value", "").css("color","#333");
	});
	jQuery("input[type='text']#global_search_field").live('blur', function() {
				jQuery(this).removeClass("active");
				if(jQuery(this).attr("value") == "") jQuery(this).attr("value", "Site search").css("color","#ccc");
	});

	/**
		Submit global search form
	*/
	jQuery('#global_search_submit, .input_search_btn').live('click', function()	{
		jQuery('#global_search_field_hidden').attr('value', jQuery('#global_search_field').attr('value'));
		jQuery('form#global_search_form').submit();
	});
	

    /*******************************************************
     Tabbed navigation items : under top menu -> sub navs
     */
     jQuery(".tab_nav").each(	function(intIndex) {

        if (jQuery(this).children("li").hasClass("active")) {
            // add/position arrow indicator
            jQuery(this).children("li.active").append("<div class='css-arrow'></div>");

            var arrow_pos = (jQuery(this).children("li.active").width() / 2) - 5;
            jQuery(this).children("li").children(".css-arrow").css("left", arrow_pos);
        }
    }
            );


/**
 	Open links in new window and close the list's fancy box in settings list on the Header at top-right
*/
	jQuery('#setting_btn_list ul li a').live('click', function() {
		var redirect_url = jQuery(this).attr('href');
		window.location.href = redirect_url;
		jQuery.fancybox.close();
	});

/*********************************************************************************************
	target="_self" alternative for XHTML 1.0 Strict/1.1
	add class "external_link" onto any <a> tag which is desired to be opened in a new window
*/
  jQuery("a.external_link").click(function(){
    window.open(this.href);
    return false;
  });

/***********************************
	Accordion function calls
*/
	jQuery('.accordion').accordion_custom();
	jQuery('.grouped').each(function() {
		jQuery('.grouped .accordion:last-child').addClass('last')
		jQuery('.grouped .accordion:last-child h2').addClass('last');
		jQuery('.grouped .accordion:last-child .acc_content').addClass('last');
    });
}); // End of JQuery Ready function

/*******************************************************************************
	make function calls for rounded-corners at window.load time
*/
jQuery(window).load(function () {

	if (jQuery.browser.msie && jQuery.browser.version < 9) {
		
		jQuery('.pieInject').pieInject(
				'#content_primary .accordion h2, button, .button, h2.grouped_title, h2.search_triggers, .acc_container, .accordion .last, #view_settings_btn, .round_bottom'
		);

        if ((parseInt(jQuery.browser.version.split('.')[0], 10) <= 6)) {
            jQuery('.close_settings_btn').live('click', function() {
                jQuery.fancybox.close();
            });
        }
	}
});
