// JavaScript Document
/* 
 *author:Aakash Bapna(http://aakash.me)
 */

if(typeof jQuery=="undefined"){
	
	var ele=document.createElement("script");
	ele.type="text/javascript";
	ele.src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
	document.getElementsByTagName("head")[0].appendChild(ele);
	var interval = setInterval(function() {
      if (typeof jQuery != 'undefined') {
        clearInterval(interval);
        jQuery.noConflict();//prototype and other lib compatability fix.
      }
    }, 50);
	
	
	}
	
	var wp_likes={};
	wp_likes.endpoint="/wp-content/plugins/wp-likes/endpoint.php";
	wp_likes.method="like";
	wp_likes.didPrevFinish=true;
	wp_likes.makeCall=function(post_id,callback,isUnlike){
		if(!this.didPrevFinish)return false;
		if(isUnlike==true)this.method="unlike"; else this.method="like";
		params={};
		params["method"]=this.method;
		params["post_id"]=post_id;
		jQuery.ajax({
			type:"GET",
			url:this.endpoint,
			data:params,
			beforeSend:function(){
			this.didPrevFinish=false;	
			},
			success:function(response){
				if(response.success==true){
					callback(response);
					alert('Váš hlas byl přidán, děkujeme.');
				}
				else {
					//error in call
					alert('Opakovaně hlasovat v soutěži lze jednou za hodinu.');
					wp_likes.log("unsuccessfull request, response from server:"+ response)
					
				}
				
			},
			error:function (xhr, ajaxOptions, thrownError){
                wp_likes.log('error in AJAX request.');
				wp_likes.log('xhrObj:'+xhr);
				wp_likes.log('thrownError:'+thrownError);
				wp_likes.log('ajaxOptions:'+ajaxOptions);
                                                },
			complete:function(){
					this.didPrevFinish=true;
					window.location.reload( false );
			},
			dataType:"json",
			cache:false
			
		})
		
	}
	wp_likes.like=function(post_id){
		wp_likes.log("like click for post- "+post_id);
		jQuery("#wp_likes_post-"+post_id+" a.like").fadeTo("slow",.2);
		this.makeCall(post_id,function(response){
			var postDom=jQuery(document.getElementById("wp_likes_post-"+post_id));
			postDom.children("span.text").html(response.likeText);
			var thumbImg=postDom.children("a.like").children("img");
			postDom.children("a.like").attr('title',"").removeAttr('href').text("").addClass("liked").removeClass("like");
			thumbImg.appendTo(postDom.children("a.liked").eq(0));
			postDom.children("a.liked").fadeTo("slow",.80);
			postDom.children("div.unlike").show("fast");
		},false);
		
	}
    wp_likes.unlike=function(post_id){
	wp_likes.log("unlike click for post- "+post_id);
	jQuery("#wp_likes_post-"+post_id+" a.liked").fadeTo("slow",.2);
	this.makeCall(post_id,function(response){
		var postDom=jQuery(document.getElementById("wp_likes_post-"+post_id));
		postDom.children("span.text").html(response.likeText);
		postDom.children("a.liked").attr("href","javascript:wp_likes.like("+post_id+")").addClass("like").removeClass("liked").fadeTo("slow",.95);
		postDom.children("div.unlike").hide("fast");
	},true)
	
	
}
	wp_likes.log=function(obj){
		if(typeof console !="undefined")console.log(obj);
	}
// -->

