/**
 * @author chegham wassim
 * @version 2.0
 * @website cheghamwassim.com
 */
var EE_ON = false;
var EE_T = 1000;
var EE_D = 500;
var t = null;

// @deprecated
function ee_1(){
	
	function ee(){
		jQuery('div#logo-2').fadeIn(EE_T, function(){
			jQuery(this).fadeOut(EE_T, function(){
				if( EE_ON == false ) {
					jQuery('div#logo-2').stop(true, false).show(EE_D);
				}
				else ee();
			});
		});
	}
	
	//
	ee();
	jQuery('div#header').append('<div id="android" class="step-1"></div>');
	jQuery('div#android.step-1').delay(500).animate({"top":200}, 1000, function(){
		jQuery(this).removeClass('step-1').addClass('step-2');
		jQuery(this).delay(600).animate({'top':40}, 3000, function(){
			jQuery(this).removeClass('step-2').addClass('step-3');
			jQuery(this).delay(1000).animate({'top':'-400'}, 3000, function(){
				jQuery(this).removeClass('step-3').addClass('step-4');
				jQuery(this).animate({'top':'-42'}, 1000, function(){
					//jQuery(this).delay(60000).remove();
				});
			});
			jQuery('#beta').delay(1000).animate({'top':'-400'}, 3000, function(){
				jQuery(this).hide(100, function(){
					jQuery(this).css({'top':70});
					jQuery(this).delay(1000).fadeIn();
				});
			});
		});
	});
}

function _bind_enlarge()
{
	
	jQuery('.enlarge').click(function(e){
		
		e.preventDefault();
		
		var a = jQuery(this);
		var image_url = jQuery(this).attr('href'); // choose "href" first
		var tmp = image_url.split('.');
		var ext = tmp[tmp.length-1];
		if( ext != "jpg" && ext != "jpeg" && ext != "png" )
		{
			image_url = jQuery(this).attr('rel'); // otherwise choose "rel"
		}
		
		jQuery.ajax({
			url: image_url,
			beforeSend: function(){
				jQuery('<div id="loading-dialog"></div>').appendTo(a);
			},
			success: function(){
				if ( jQuery('#large-image').length === 0 ){
					jQuery("<div id='large-image'>\
						<img id='big-image' src='"+image_url+"' alt='' title='Close'/>\
					</div>").appendTo('body');
				} 
				else {
					jQuery('#large-image img').attr('src', image_url);
					jQuery('#large-image').show();
				}
				
				jQuery('#big-image').css({'height': jQuery(window).height()-65});
				
				a.find('#loading-dialog').remove();
				
			},
			complete: function(){
				
			},
			error: function(){
				a.find('#loading-dialog').remove();
			}
		});

	});
}

function _bind_close(){	
	var img = jQuery('#big-image');		
	jQuery('div#large-image').hide(100, function(){
		img.attr('src', 'site/assets/v2/img/loader3.gif');
	});
	
}

function _bind_show_next(){
	_load_picture(1);	
}

function _bind_show_previous(){
	_load_picture(-1);	
}

function _load_picture(n){
	// get the current image src
	var current_img = jQuery('#large-image img');
	
	var current_url = current_img.attr('src');
	var img = jQuery('.polaroid[rel="'+current_url+'"] img') || '';
	var current_img_index = parseInt(img.attr('id').replace('fb-img-', ''));
	var next_img_index = current_img_index+n;
	var next_img = jQuery('img#fb-img-'+next_img_index).closest('.polaroid');
	
	if ( next_img !== null && next_img.length > 0 && next_img.attr('rel') !== current_url  ){
		
		//current_img.attr('src', '/site/assets/v2/img/loader3.gif');
		
		current_img.fadeOut(100, function(){
			
			current_url = next_img.attr('rel');

			var imageObj = new Image();
	    imageObj.src = current_url;
	    jQuery(imageObj).load( function() {
				
				current_img.attr('src', current_url).fadeIn();	
				delete imageObj;
				
			});
				
		});	
	}
}

function _bind_kb(evt){
	evt.preventDefault();
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
    switch (evt.keyCode) {
    	case 27:
    		_bind_close();
    	break;
      case 37:
      	console.info('show previous picture');
        _bind_show_previous();
      break;    
      case 39:
      	console.info('show next picture');
				_bind_show_next();      
			break;    
   }
	}
};

function ee_0(){
	jQuery('div#android').stop(true, false).fadeOut(EE_D, function(){
		jQuery(this).remove();
	});
	jQuery('#beta').stop(true, false).fadeIn(EE_T, function(){
		jQuery(this).css({'top':70});
	});
	jQuery('div#logo-2').stop(true, false).show();
}

function _bind_renew_token(){
	jQuery('a.renew-token').click(function(e){
		
		e.preventDefault();
		
		var url = jQuery(this).attr('href');
		
		jQuery.ajax({
			url:url,
			type:'POST',
			dataType:'json',
			beforeSend:function(){},
			success:function(o){
				if ( o.response && o.response == 200 ){
					var _txt = (o.spam_a+' + ? = '+o.spam_c);
					jQuery('.spam-question').text( _txt );
					jQuery('.spamfilter').attr('placeholder', _txt);
				}
			},
			complete:function(o){
			},
			error:function(){}
		});
		
	});
};

function _enlarge_code(){
	
	var _code = jQuery('#post-content pre');
	if ( _code.length > 0 ){
		
		var _code_height = parseInt(_code.css('height').replace('px',''));
		var _cc = _code.find('.codecolorer-container');
		var _cc_h = _cc.css('height');
		var _window_height =  jQuery(window).height();
		var _h = _window_height+'px';
	
		if ( _code_height > _window_height ){
			
			_code.prepend('<span class="enlarge-code">Toggle</span>');
			jQuery('.enlarge-code').live('click', function(){
				
				if ( _code.toggleClass('big-code').is('.big-code') ){
					_cc.css({'height': _h});
					_code.css({'height': _h});
				}
				else {
					_code.css({'height': _cc_h});
					_cc.css({'height': _cc_h});
				}
				
			});
		}
		
	}
	
};

function _load_category(){
	
	jQuery('.categories-links').bind('click', function(e){
		
		var _link = jQuery(this).attr('href').split('/');
		var _category = _link[ _link.length-1 ];
		var _lis = jQuery('#entries-list li');
		if ( _lis.length === 0 ){
			window.location = 'blog/catgeory/'+_category;
		}
		else {
			
	
			e.preventDefault();
			var _result = [];
			var _title = jQuery('#main > .big-title').text();
			
			jQuery('#main > .big-title').text('Category : '+_category);
			
			jQuery('#entries-list').addClass('loading');
			window.location.hash = undefined;
			var fadeInSpeed = 500; // speed in ms
			_lis.each(function(i){
				jQuery(this).delay(i*(fadeInSpeed/4)).animate({'opacity':'0'}, fadeInSpeed);
			});
			jQuery('#entries-list').load('/blog/category', {'ajax':1, 'category':_category}, function(){
				window.location.hash = 'category/'+_category;
				_lis.each(function(i){
					jQuery(this).delay(i*(fadeInSpeed/4)).animate({'opacity':'1'}, fadeInSpeed);
				});
				return false;
			});
			
			
		}	
		
	});
	
};

function _ajax_search(){
  
  var _res_container = jQuery('#entries-list');
  var _form = jQuery('#searchform');
  
  jQuery('#sidebar #s').bind('keyup', function(){

    var _search = jQuery(this).val();
    
    if ( _search.length > 1){
      
      if ( _res_container.length > 0 ){
        
        _res_container.addClass('loading');
        _search_xhr = _res_container.load( _form.attr('action') , {'s':_search, 'ajax':1}, function(){
          
          _res_container.removeClass('loading');
          
          var count = _res_container.find('li').length;
          
          jQuery('.big-title').html('Search Result for <span class="search-terms">"'+_search+'"</span>');
          jQuery('.big-title').next('h2').html(count+' '+( count > 1 ? 'entries' : 'entry' )+' found');
          
        });
        
      }
      
    }
    else {
      
      _res_container.find('li').remove();
      
    }
    
  });
  
};

var _h1 = function(){
	
	var _titles = jQuery('#post-body h1');
	
	if ( _titles.length > 0 ){
		
		_titles.nextUntil('h1').hide();
		
		_titles.bind('click', function(){
			
			_titles.not(this).nextUntil('h1').slideUp();
			
			jQuery(this).nextUntil('h1').slideToggle();
			
		});
		
	}
	
	
};

jQuery(document).ready(function(){
	
	_h1();
	_ajax_search();
	_load_category();
	_bind_renew_token();
	_enlarge_code();
	_bind_enlarge();
	
	jQuery('#body').bind('keyup', function(e){ _bind_kb(e); });

	jQuery('#big-image').live('click', function(){
		_bind_close();		
	});
		
	jQuery('.disabled').click(function(e){e.preventDefault();});
	
	jQuery('div#ee').click(function(){
		EE_ON = !EE_ON;
		if( EE_ON ) ee_1();
		else ee_0();
	});
	jQuery('div#frame a').hover(function(){
		jQuery(this).find('span').stop(true, false).animate({'bottom':'0px'}, 500);
	}, function(){
			jQuery(this).find('span').stop(true, false).animate({'bottom':'-60px'}, 800);
	});
	jQuery('div#apps li div.thumb a').hover(function(){
		jQuery(this).find('img').animate({'opacity': '0.2'});
	}, function(){
		jQuery(this).find('img').animate({'opacity': '1'});
	});
});




