$().ready( function() {
	
	var $urlbase = $('base').attr('href');
	
	if( $( 'textarea.cols' ).attr( 'class' ) != undefined )
		$( 'textarea.cols' ).fck( { path: $( 'base' ).attr( 'href' ) + 'fckeditor/' , toolbar: 'Basic' } );
	
	if( $( 'textarea.caption' ).attr( 'class' ) != undefined )
		$( 'textarea.caption' ).fck( { path: $( 'base' ).attr( 'href' ) + 'fckeditor/' , toolbar: 'Caption' , height: 160 } );

	$( '.menu_principal' ).mouseenter( function() {
		$( this ).find( 'ul' ).show();
		$( this ).find( '.down_arrow' ).removeClass( 'down_arrow' ).addClass( 'up_arrow' );
	} );

	$( '.menu_principal' ).mouseleave( function() {
		$( this ).find( 'ul' ).hide();
		$( this ).find( '.up_arrow' ).removeClass( 'up_arrow' ).addClass( 'down_arrow' );
	} );
	
	
	$( '.menu_composto' ).mouseenter( function() {
		$( this ).find( 'ul' ).show();
		$( this ).find( '.down_arrow' ).removeClass( 'down_arrow' ).addClass( 'up_arrow' );
	} );
	
	$( '.menu_composto' ).mouseleave( function() {
		$( this ).find( 'ul' ).hide();
		$( this ).find( '.up_arrow' ).removeClass( 'up_arrow' ).addClass( 'down_arrow' );
	} );
	
	if( $( '.fullview' ).attr( 'class' ) != undefined ) {
		fullview( this );
	}
	
	
	var COOKIE_NAME = 'mmbb_res';
	if ( $.cookie( COOKIE_NAME ) == null ) {
		COOKIE_NAME = 'mmbb_res';
        var options = { path: '/' , expires: 10 };
        $.cookie( COOKIE_NAME , screen.width , options );
		window.location.href = window.location.href;
	}
	
	/*if( $('input.up_img').attr( 'class' ) == 'up_img' ){
		$("input.up_img").makeAsyncUploader({
		            upload_url: $('base').attr('href') + "index.php/publisher/images/upload/", // Important! This isn't a directory, it's a HANDLER such as an ASP.NET MVC action method, or a PHP file, or a Classic ASP file, or an ASP.NET .ASHX handler. The handler should save the file to disk (or database).
		            flash_url: $('base').attr('href') + 'public/js/progressbar/swfupload.swf',
		            button_image_url: $('base').attr('href') + 'public/js/progressbar/blankButton.png'
					//debug: true
		        });
	}*/
	
	if( $( '.img_project' ).hasClass( 'img_project' ) ) {
		$( '.img_project' ).mouseenter(function(){
			$( '#filters' ).hide();
			$( '#p_name' ).html( $( this ).attr( 'alt' ) );
			$( '#p_name' ).show();
		});
		$( '.img_project' ).mouseleave(function(){
			$( '#p_name' ).hide();
			$( '#p_name' ).html( '&nbsp;' );
			$( '#filters' ).show();
		});
	}
	

	verifResolution();
	
	// swfupload configs
	if( $('.up_img').attr( 'class' ) == 'up_img' ){
		var urlbase = $('base').attr('href');
		$.getScript( urlbase + 'public/js/swfobject.js', function(){
			$.getScript( urlbase + 'public/js/jquery.uploadify.js', function(){
				$("#up_img").uploadify({
					pagePad: location.pathname,
					'uploader'	   : urlbase + 'public/js/uploadify/uploader.swf',
					'cancelImg'    : urlbase + 'public/js/uploadify/cancel.png',
					'script'       : urlbase + 'public/js/upload.php',
					'fileDataName' : 'image',
					'multi'		   : false,
					'auto'		   : true,
					'method'	   :'POST',
					'fileDesc'	   : 'Apenas imagens (.jpg, .gif, .png, .tif/tiff)',
					'fileExt'	   : '*.jpg;*.jpeg;*.gif;*.png;*.tif;*.tiff',
					'sizeLimit'    : '10485760',
					'buttonImg'    : urlbase + 'public/images/upload.gif',
					'height'	   : 18,
					'width'        : 80,
					'queueID'      : 'fileQueue',
					'folder'	   : $( '#up_img' ).attr( 'title' ) + 'public/tmp',
					'onError' 	   : function(event, queueID, fileObj, errorObj) { alert(errorObj.type + ' ' + errorObj.info ); },
					'onComplete'   : function(event,queueID,fileObj,response) { $( '#fileQueue' ).html( fileObj.name + ' - enviado com sucesso.' ); $( '#image' ).val(fileObj.name);$( '#btnSubmit' ).attr('disabled',''); }
				});
				var pagePath = settings.pagePad;
	             pagePath = pagePath.split('/');
	             pagePath.pop();
	             pagePath = pagePath.join('/') + '/';
			});
		});
	}
	
	
	
	
	if ( $( '.sortable' ).hasClass( 'sortable' ) ) {
		
		$.getScript( $urlbase + 'public/js/jquery.ui.js', function(){

			$( '.sortable' ).sortable( {
				forceHelperSize: true,
				placeholder: 'ui-state-highlight',
				update: function() {
					$( '.sortable' ).find( 'tr' ).removeClass( 'even' );
					$( '.sortable' ).find( 'tr' ).each( function( i ) {
						if (i%2!=0) $( this ).addClass( 'even' );
					} );
					var $url = $( '.sortable' ).attr( 'alt' );
					var $idToAttay = $( '.sortable' ).sortable( 'toArray' );
					var $ids = "";
					for ( i=0 ; i<$idToAttay.length ; i++ ) {
						if ( i == $idToAttay.length-1 )
							$ids += $idToAttay[i];
						else
							$ids += $idToAttay[i] + ',';
					}
					$.post( $urlbase + 'index.php/' + $url ,  { ids : $ids } );
					
				}
			} );
			$( '.sortable' ).disableSelection();

		});
		
	}
	
	

} );


function fullview()
{
	$( '#fv_img' ).html( '<img src="' + $( '#fv_current_img' ).val() + '" />' );
	
	$( '#fv_img' ).find( 'img' ).load( function() {
		var img_xy = parseFloat( $( this ).width() ) / parseFloat( $( this ).height() );
		var win_xy = parseFloat( $( window ).width() ) / parseFloat( $( window ).height() );

		if( img_xy >= win_xy ){
			$( this ).attr( 'width' , $( window ).width() );
		}else if( img_xy < win_xy ){
			$( this ).attr( 'height' , $( window ).height() );
		}

		var margin_top = ( parseInt( $( window ).height() ) - parseInt( $( this ).height() ) ) / 2;
		$( this ).css( 'margin-top' , margin_top + 'px');
		//$( '#fv_img' ).css('visibility','visible');

		$( '#fv_loading' ).hide();
		if( $( '#fv_prev_img' ).attr( 'id' ) == 'fv_prev_img' ){
			$( '#fv_prev_img_c' ).hide();
			$( '#fv_prev_img_c' ).html( '<img src="' + $( '#fv_prev_img' ).val() + '" />' );
			$( '#fv_prev_img_c' ).find( 'img' ).load( function() {
				$( this ).hide();
			} );
		}
		
		if( $( '#fv_next_img' ).attr( 'id' ) == 'fv_next_img' ){
			$( '#fv_next_img_c' ).hide();
			$( '#fv_next_img_c' ).html( '<img src="' + $( '#fv_next_img' ).val() + '" />' );
			$( '#fv_next_img_c' ).find( 'img' ).load( function() {
				$( this ).hide();
			} );
		}
		
	} );
}

function fv_setHeight( h )
{
	//$( 'body' ).height( h );
	//$( '#fv_content' ).height( h );
}

function verifResolution()
{
	if( screen.width <= 1024 ){
		$( '#window_size' ).width( 1008 ).height( 568 );
		$( '.res_1024' ).css( 'font-weight' , 'bold' );
	}else if( screen.width > 1024 && screen.width <= 1280 ){
		$( '#window_size' ).width( 1152 ).height( 628 );
		$( '.res_1280' ).css( 'font-weight' , 'bold' );
	}else if( screen.width > 1280 && screen.width <= 1440 ){
		$( '#window_size' ).width( 1296 ).height( 698 );
		$( '.res_1440' ).css( 'font-weight' , 'bold' );
	}else if( screen.width > 1440 && screen.width <= 1680 ){
		$( '#window_size' ).width( 1440 ).height( 748 );
		$( '.res_1680' ).css( 'font-weight' , 'bold' );
	}else if( screen.width > 1680 ){
		$( '#window_size' ).width( 1584 ).height( 828 );
		$( '.res_1920' ).css( 'font-weight' , 'bold' );
	}
	
	$( '.res_1024' ).mouseenter( function() {
		$( '#window_size' ).stop().animate( { width : '1008px' , height : '568px' } , 1000 );
	} );

	$( '.res_1280' ).mouseenter( function() {
		$( '#window_size' ).stop().animate( { width : '1152px' , height : '628px' } , 1000 );
	} );

	$( '.res_1440' ).mouseenter( function() {
		$( '#window_size' ).stop().animate( { width : '1296px' , height : '698px' } , 1000 );
	} );
	
	$( '.res_1680' ).mouseenter( function() {
		$( '#window_size' ).stop().animate( { width : '1440px' , height : '748px' } , 1000 );
	} );
	
	$( '.res_1920' ).mouseenter( function() {
		$( '#window_size' ).stop().animate( { width : '1584px' , height : '828px' } , 1000 );
	} );
}

