/*
    General JavaScript Module
    © 2009 www.ajaxBlender.com
    For any questions please contact us at support@ajaxBlender.com
*/

var AKPC_IDS = '';

/*  Cufon Options */

Cufon.replace(['#sidebar h2','.common-list h1','#sidebar a.view-all','#content .updates ul h2','button.wpsc_buy_button','.single_product_display .links strong a','#content .updates h2','#content .tutorials .post-list .entry strong.read-more','#content .tutorials .post-list .entry h2 a','#content .tutorials .post-list h2.title','#content .tutorials h3.title','#content h3','.tabs ul li span span','#cats-by-frameworks h2','.copy span','#content h4','#content .leave-a-comment strong.read-more'], {
	hover: true
}); 
Cufon.replace('#content A.new-vertion', { textShadow: '1px 1px #FFFFFF'});

/* ~Cufon Options */

$(document).ready(function() {
    $('.tabs UL LI A.tab').click(function(){
        $('.tabs UL LI').removeClass('updates-selected').removeClass('popular-selected').removeClass('tutorials-selected');
        $('.loader').height($('.main-content .updates').height());
        $('.loader').show();
        $(this).parent().addClass($(this).attr('rel') + '-selected');    
        $('.main-content .updates').load($(this).attr('href'), {ajax:1, sort:$(this).attr('rel')}, function(){
            $('.loader').hide();
            update_sifr();
        });
        return false;
    });
    
    $('#search input').keyup(function(e){
        var code = e.keyCode;       
        if (code > 36 && code <= 40){ return false;}
        if (code == 27){
            $('#search-ajax').slideUp('normal');
            return false;
        }
        setTimeout('get_words("' + $(this).val() + '");', 800);
    });    
    
    
    var imageLoadingUri = '/wp-content/themes/ajaxBlender/public/images/lightbox/lightbox-ico-loading.gif';
    $('.screenshots a').lightBox({imageLoading: imageLoadingUri});
    $('a.wpGallery').lightBox({imageLoading: imageLoadingUri});
    
    
    initCommentForm();
    
	$('input[type="text"][value!=""]').bind('focus', function() { if(this.value == this.title) { this.value = '' } });   
	$('input[type="text"][value!=""]').bind('blur', function() { if(this.value == '') { this.value = this.title } });   
	
	initViewHint();
	
	$('A.wpsc_buy_button_link').click(function(){
		//$(this).parent().parent().parent().parent().submit();
		$(this).parent().parent().parent().children('BUTTON.wpsc_buy_button').click();
	});
});
	


function updateCommentForm(){
    var author = $('.comments .form INPUT#author');
    var email = $('.comments .form INPUT#email');
    
    if (author.val() != author.attr('title')) {
        $('.comments #you-name').html(author.val());
    }
    
    if (email.val() != '' && email.val() != email.attr('title')) {
        $('#avatar .loader').show();
        //$('#avatar').load(window.location.href, { ajax_email:email.val() }, function(){ $('#avatar .loader').hide(); });
        $('#avatar').load($('INPUT#avatar_path').val(), { ajax_email:email.val(), base_path:$('INPUT#base_path').val() }, function(){ $('#avatar .loader').hide(); });
    }
    
    $('.comments .form INPUT, .comments .form TEXTAREA').each(function(){ setValue($(this)); });
    $('.comments .form INPUT, .comments .form TEXTAREA').click(function(){ clearValue($(this)); });
    $('.comments .form INPUT, .comments .form TEXTAREA').focus(function(){ clearValue($(this)); });
    $('.comments .form INPUT, .comments .form TEXTAREA').keydown(function(){ clearValue($(this)); });
    $('.comments .form INPUT, .comments .form TEXTAREA').blur(function(){ setValue($(this)); });
    
    $('.comments .form INPUT#author').keyup(function(){ 
        if ($(this).val() == ''){
            $('.comments #you-name').html('Your name');
        } else {
            $('.comments #you-name').html($(this).val());
        }
    });
    
    $('.comments .form INPUT#email').blur(function(){
        if ($(this).val() != '' && $(this).val() != $(this).attr('title')){
            $('#avatar .loader').show();
            //$('#avatar').load(window.location.href, { ajax_email:$(this).val() }, function(){ $('#avatar .loader').hide(); });
			$('#avatar').load($('INPUT#avatar_path').val(), { ajax_email:email.val(), base_path:$('INPUT#base_path').val() }, function(){ $('#avatar .loader').hide(); });
        }
    });
    
    $('.comments .form INPUT, .comments .form TEXTAREA').each(function(){
        if ($(this).val() != $(this).attr('title')){
            $(this).css('color','#333333');
        }
    });
    
    /*sIFR.activate(serifa);
    sIFR.replace(serifa, {
	  selector: '#content strong.read-more ',
	  wmode: 'transparent',
	  css: [
	  		'.sIFR-root {font-size: 15px; font-weight: bold; cursor: pointer; text-align: center;}' ,
	        'a { color: #FFFFFF; text-decoration: none;}', 
	        'a:hover {color: #FFFFFF; text-decoration: none;}'
	       ]
	});*/
	Cufon.refresh();
}

function clearValue(el){
    if (el.val() == el.attr('title')){
		el.val('').css('color','#333333');
    }		
}

function setValue(el) {
    if (el.val() == '' ){
		el.val(el.attr('title')).css('color','#999999');
    }			
}

function disable() {
	$('.comments .form INPUT, .comments .form TEXTAREA').each(function(el){ 
	    $(el).attr('disabled', true);	
	    $('#submit').attr('disabled', true);	
	});	
}

function enable() {
	$('.comments .form INPUT, .comments .form TEXTAREA').each(function(el){ 
	    $(el).attr('disabled', false);	
	    $('#submit').attr('disabled', false);	
	});	
}

function update_sifr() {
	Cufon.refresh();
    /*var serifa = { src: '/wp-content/themes/ajaxBlender/public/swf/serifa-bt.swf' };
    sIFR.activate(serifa);
    sIFR.replace(serifa, {
      selector: '#content .updates H2',
      wmode: 'transparent',
      css: ['.sIFR-root {font-size: 21px; color: #4B3F30; font-weight: bold;}' ,'a {color: #4B3F30; text-decoration: underline;}', 'a:hover {color: #4B3F30; text-decoration: none;}']
    });*/
}

function get_words(str) {
    if ($('#search input[name="s"]').val() == str) {
        $('#search-ajax').load('/',{search:str}, function() {
            if ($('#search-ajax LI').size() > 0) {
                $('#search-ajax').slideDown('normal');
                
                $('body').click(function() {
                    $('#search-ajax').slideUp('normal');
                });
                
                $('#search-ajax UL LI A').click(function() {
                    $('#search input[name="s"]').val($(this).attr('rel'));
                    $('#search-ajax').slideUp('normal');
                    
                    return false;
                });
            }
        });
    }
}

function initCommentForm() {
    updateCommentForm();
	$('#commentform').submit(function(){
		$('.comments .form INPUT, .comments .form TEXTAREA').each(function(){ clearValue($(this)); });
		disable($(this));
        var form = $(this);
		var formData = $(this).serialize();        
		var error = '';        
        $('#error').hide();
        
        $('.loader').show('fast', function(){
        	$.ajax({
				type: 'POST', url: form.attr('action'), data: formData,
	            complete : function (response) {
	            	$('.loader').hide();

					error = response.responseText;
					error = error.split(':');
					
	               if (error[0].toLowerCase() == 'error') {
	                	$('#error').html(error[1]);
	                    $('#error').show();
						ajax_refrech_code();
					} else{
						document.getElementById('commentform').reset();		
	                    $('#comments-list').load(window.location.href, {get_comments : 1, id : $('.comments .form input[name="comment_post_ID"]').val()}
	                    ,function(){initCommentForm();});
	                    $('.comments .form INPUT, .comments .form TEXTAREA').css('color','#999999');
					}
	                $('.comments .form INPUT, .comments .form TEXTAREA').each(function(){ setValue($(this)); });
	                enable($(this));
				}
			});
        });
		
		return false;
	});
}

function my_process_comments(str){
	list = str.split(':');
	if(list[0].toLowerCase() == 'error'){
		return list[1];
	} else {
		return '';
	}
}

function initViewHint() {
	$('.view-hint').mouseenter(function() {
		$( $(this).attr('rel') ).css({top: $(this).position().top + $(this).height() + 5, left: $(this).position().left });
		$( $(this).attr('rel') ).slideDown();
	});
	
	$('.view-hint').mouseleave(function() {
		$( $(this).attr('rel') ).hide();
	});
}
