/*	###########################################################################
	42 B.V.
	date:   August 2009
	author: C. Egor Kloos
	author: ---
	########################################################################### */

    $(document).ready(function(){
        
        if($("html").hasClass('.no-borderradius')) {
            //extended DD_roundies to add rules to UI corner classes

            $.uicornerfix = function(r){
            	DD_roundies.addRule('#content .ui-corner-all, .ui-state-error, .ui-button', r+'');
            	// fuck's up styling of next selected tab/accordion
            	//DD_roundies.addRule('.ui-corner-top', r+' '+r+' 0 0');
            	//DD_roundies.addRule('.ui-corner-bottom', '0 0 '+r+' '+r);
            };

            $.getScript("/info/Corporate/scripts/DD_roundies.js", function(){
                 $.uicornerfix('5px');
            });
            $("head").append('<link type="text/css" href="/info/Corporate/styles/hack.css" rel="stylesheet" media="screen" />');
        }
                        
        // Accordion

        $(".accordion").accordion({ 
            header: "h3", 
            active: uiIndex(".accordion>div"),
            collapsible: true,
            clearStyle: false
        });		

		
				// Apply page Accordion
        $(".applyaccordion").accordion({ 
            header: "h2", 
            active: uiIndex(".applyaccordion>div"),
            collapsible: true,
            clearStyle: true
        });        
        
        $('li.open ul').show();
        
        
				// Top Navigation pulldown
        $('#navigation>ul>li,#navigation>ul>li>ul>li').hover(
               function() { $(this).addClass('hover'); },
               function() { $(this).removeClass('hover'); }
        );        
        
        // Tabs
        $('#tabs').tabs({
            selected: uiIndex("#tabs>ul>li")
        });
        
        // Dialog			
        $('#dialog').dialog({
        	autoOpen: false,
        	width: 600,
        	buttons: {
        		"Ok": function() { 
        			$(this).dialog("close"); 
        		}, 
        		"Cancel": function() { 
        			$(this).dialog("close"); 
        		} 
        	}
        });
        
        // Dialog Link
        $('#dialog_link').click(function(){ 
        	$('#dialog').dialog('open');
        	return false;
        });
        
        // Datepicker
        $('#datepicker').datepicker({
        	inline: true
        });
        
        // Slider
        $('#slider').slider({
        	range: true,
        	values: [17, 67]
        });
        
        // Progressbar
        $("#progressbar").progressbar({
        	value: 20 
        });
        
        //hover states on the static widgets
        $('#dialog_link, ul#icons li').hover(
        	function() { $(this).addClass('ui-state-hover'); }, 
        	function() { $(this).removeClass('ui-state-hover'); }
        );
        
        // Return the index of an 'active' element
        function uiIndex(arr){
            var e = $(arr);
            var i = false;
            e.each(function(index){
                if($(this).hasClass('active')) { i = index }
            })
            $(this).find('ul').toggle('slow');
            /* I wish 'haslayout' would just die! Also removes silly height! */
            $('.ui-accordion-content').attr('style','zoom:1')
            return i;
        }
        
        $("tbody tr:odd").addClass("odd");
        $("thead tr th").addClass("ui-state-default");
        $("thead tr th:first").addClass("ui-corner-left");
        $("thead tr th:last").addClass("ui-corner-right");

               
        $(".buttons a.ui-corner-all").click(function(){
            $(this).submit();
        })
        
        // Run validation on all forms
        function runValidation(){
		   if($(':input').length<=0) return false;
		   if(typeof(errorAlert)=="undefined") return false;		
            $(':input[name="required[]"]').attr('name',function(index){return "required[" + index + "]"}); 
            $("label span.required").parent().next(":input").addClass("required");
            $('#content').prepend('<div class="ui-state-error ui-corner-all" id="form-error"><p><span class="ui-icon ui-icon-alert"></span>'+errorAlert+'</p></div>')
            $("#form-error").hide();
            $("form").each(function(){$(this).validate({
                errorContainer: "#form-error",
                showErrors: function(errorMap, errorList) {
                    $("#error-count").html(this.numberOfInvalids());
                    this.defaultShowErrors();
                }
            })});
        }
        runValidation()
    })
