/*
jQuery code used by Sofa Disordery theme. Feel free to modify in order to fit your needs!
*/

jQuery( document ).ready( function() {
	
	// jcookie used to remember main navig state; open or closed?
	if( jQuery.cookie( 'cookactiv' ) == null ) {
		jQuery( '#openclosecontent' ).show();
		jQuery.cookie( 'cookactiv', 'isopened' );
	} else if( jQuery.cookie( 'cookactiv' ) == 'isopened' ) {
		jQuery( '#openclosecontent' ).show();
	} else if( jQuery.cookie( 'cookactiv' ) == 'isclosed' ) {
		jQuery( '#openclosecontent' ).hide();
	}
	jQuery( '#toggler' ).click( function() {
		if( jQuery.cookie( 'cookactiv' ) == 'isclosed' ) {
			jQuery( '#openclosecontent' ).slideToggle( 150, function() { 
				jQuery.cookie( 'cookactiv', 'isopened' ); 
			} );
		} else if( jQuery.cookie( 'cookactiv' ) == 'isopened' ) {
			jQuery( '#openclosecontent' ).slideToggle( 150, function() { 
				jQuery.cookie( 'cookactiv', 'isclosed' ); 
			} );
		}
	} );
	
	// main navig : find out whether main navig items have title attribute. if true, extract and append
	jQuery( '#globalnavi li' ).each( function( i ) {
		var titl = jQuery( 'a', this ).attr( 'title' );
		if( titl != '' ) jQuery( 'a:first', this ).append( '<span>' + titl + '</span>' );
	} );

	// scroll to top
	jQuery( '#totop' ).click( function() {
		var bodyelem = '';
		if( jQuery.browser.safari ) bodyelem = jQuery( 'body' ); // only Opera specific
		else bodyelem = jQuery( 'html' );
		jQuery( bodyelem ).animate( { scrollTop: jQuery( '#gettotop' ).offset().top }, 'slow' );
		return false;
	} );

} );

// images hover : used in conjunction with .shutter class, shows and hides semi-transparent captions (title + description if any)
jQuery( function() {
	// images hover
	// early document ready bugfix for webkit & opera
	if( document.readyState != 'complete' ) {
		setTimeout( arguments.callee, 100 );
		return;
	} else {
		jQuery( '.shutter' ).each( function( i ) {
			
			jQuery( 'img', this ).css( { 'margin-bottom': 0 } );
			var this_img_height = jQuery( 'img', this ).outerHeight( true );
			var this_img_width  = jQuery( 'img', this ).outerWidth( true );
			var title_fix = 0;
			
			// image margins
			if( parseInt( jQuery( 'img', this ).css( 'margin-left' ) ) > 0 ) this_img_width -= parseInt( jQuery( 'img', this ).css( 'margin-left' ) );
			if( parseInt( jQuery( 'img', this ).css( 'margin-right' ) ) > 0 ) this_img_width -= parseInt( jQuery( 'img', this ).css( 'margin-right' ) );
			// this padding
			if( parseInt( jQuery( '.shuttercaption', this ).css( 'padding-left' ) ) > 0 ) this_img_width -= parseInt( jQuery( '.shuttercaption', this ).css( 'padding-left' ) );
			if( parseInt( jQuery( '.shuttercaption', this ).css( 'padding-right' ) ) > 0 ) this_img_width -= parseInt( jQuery( '.shuttercaption', this ).css( 'padding-right' ) );
			// title
			if( jQuery( '.shuttercaption h3', this ).outerHeight( true ) > 0 ) title_fix += jQuery( '.shuttercaption h3', this ).outerHeight( true );
			if( parseInt( jQuery( '.shuttercaption', this ).css( 'padding-top' ) ) > 0 ) title_fix += parseInt( jQuery( '.shuttercaption', this ).css( 'padding-top' ) ) + 5;
			
			jQuery( this ).css( { 'height': this_img_height } );
			jQuery( '.shuttercaption', this ).css( { 'top': this_img_height - title_fix, 'width': this_img_width, 'cursor': 'default' } );
			
			var this_pos_hh = jQuery( '.shuttercaption', this ).outerHeight( true );
			var move_step_y = this_img_height - this_pos_hh;
			
			jQuery( this ).hover( function() {
				jQuery( '.shuttercaption', this ).stop().animate( { top: move_step_y }, { queue: false, duration: 150 } );
			}, function() {
				jQuery( '.shuttercaption', this ).stop().animate( { top: this_img_height - title_fix }, { queue: false, duration: 200 } );
			} );
			
		} );
		
		// wp-captions
		jQuery( 'div.wp-caption' ).each( function( xyz ) {
			var real_width = jQuery( 'img', this ).outerWidth( true );
			jQuery( this ).css( 'width', real_width );
		} );
		
	}
} );

// uniforms (do not require document ready)
jQuery( function() {
	jQuery( "input, checkbox, textarea, button" ).uniform();
} );
