// JavaScript Document
var inter='';
$(document).ready(function() {

	//initcloud();
	
	$('#form').center();
	
	if($('.sizeme').length>0){
		hei = $(document).height()-120;
		$('.sizeme').height(hei);
	}
	setInterval("initBanners()", 3000);
	$('#form_image').attr('src', '');
	$('#form_image').attr('src', 'assets/php/random_image.php');
	showfeatured(2);
	
	inter = self.setInterval( "showfeatured(2)", 10000 );

});
function initBanners(){
	$('.bchanging').each(function(index) {
		var cont = $(this);
		var thevis;
		var len = $(this).children().length;
		$(cont).children().each(function(index) {
			if($(this).is(':visible')){
				thevis = index;
			}		
		});
		
		$(cont).children().each(function(index) {
			if(index == thevis){
				$(this).hide();
				if(index == len-1){
					$(cont).children(":first").show();
				}
			}
			if(index == thevis+1){
				$(this).show();
			}

		});
	});

	

}
function openForm(){
	$('#form').center();
	$('#form_holder').fadeIn('slow');
	$('#share_options').hide();
}
function closeForm(){
	$('#form_holder').fadeOut('slow');
	$('#s_name').val('');
	$('#s_email').val('');
	$('#r_email').val('');
	$('#m_message').val('');	
	$('#form_error').hide();
	$('#form_ok').hide();
}
function toggleShare(){
	$('#share_options').toggle();
}
function toggleShares(item){
	$('#share_options'+item).toggle();
}
function initcloud(){
	var words=["aaa","bbb","ccc","dddd", "eee", "ffff", "fghjmfjhm", "aahgfa","aefzsdf","h,jmhnd","zdzdxv", "hfgjfm", "zsdfzf", "hjmj", "efdsb","bnvbn","wefaef","ddqwqedd", "bnh", "dvzc", "rgsrg"];
	var wordshz=[5,10,40,8, 10, 6, 4, 5,10,20,8, 10, 6, 4, 5,40,20,8, 10, 6, 4];
	var maxsize = 15;
	var minsize = 9;
	var themax = 0;
	var themin = 100000;
	$(wordshz).each(function(index) {
		if(wordshz[index]>themax){
			themax = wordshz[index];
		}
		if(wordshz[index]<themin){
			themin = wordshz[index];
		}
	});
		
	$(words).each(function(index) {
		var thesize = minsize + Math.ceil((maxsize-minsize)*wordshz[index]/themax);
		//var thickness = 300+Math.floor(3*wordshz[index]/themax)*100;
		var thickness = 400;
		$('#dynacloud').append('<a style="font-size: ' +thesize+ 'px; font-weight:'+thickness+'">' + words[index] + '</a>&nbsp; ');
	});
}
function backTop(){
	 $(window).scrollTo( { top:0, left:0 }, 500);
}


jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - 310 ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - 250 ) / 2 + "px");
	return this;
}


function sendamail() { 
	var s_name = $('#s_name').val();
	var s_email = $('#s_email').val();
	var r_email = $('#r_email').val();
	var m_message = $('#m_message').val();
   	var m_href = window.location.href;
   	var m_loc = $(document).attr("title");
  
	$.ajax({
		type: "POST",
		url: "./assets/php/sendMessage.php",
		data: {s_name:s_name,s_email:s_email,r_email:r_email,m_message:m_message,m_href:m_href,m_loc:m_loc},
		error: function(msg){
			$('#form_error').show();
		},
		success: function(msg){
			$('#form_ok').show();
		}
	});
   	
}
function cleanme(target){
	var f_name = $('#form_firstname').val();
	var f_website = $('#form_website').val();
	var f_comment = $('#form_comment').val();
	
	if(f_name==''){
		$('#form_firstname').val('Name (required)');
	}
	if(f_comment==''){
		$('#form_comment').val('Write your comment here');
	}
	if(f_website==''){
		$('#form_website').val('Website (e.g. www.mywebsite.com)');
	}
	if($(target).val()=='Name (required)' || $(target).val()=='Website (e.g. www.mywebsite.com)' || $(target).val()=='Write your comment here'){
		$(target).val('');
	}
}
function sendcomment() { 
	var f_name = $('#form_firstname').val();
	var f_website = $('#form_website').val();
	var f_comment = $('#form_comment').val();
	var f_vercode = $('#form_vercode').val();
	var f_parent = $('#form_parent').val();
	var f_code = $('#form_code').val();
	var allgood = 1;
	if(f_name=='' || f_name=='Name (required)'){
		$('#form_notify').html('Your name is required');
		allgood=0;
	}
	if(f_comment=='' || f_comment=='Write your comment here'){
		$('#form_notify').html('Comment text is required');
		allgood=0;
	}	
	if((f_name=='' || f_name=='Name (required)') && (f_comment=='' || f_comment=='Write your comment here')){
		$('#form_notify').html('Not all required fields are filled');
		allgood=0;
	}	
	if(f_vercode==''){
		$('#form_notify').html('Security code is required');
		allgood=0;
	}
		//alert(hex_md5(f_vercode));
	if(allgood==1){
	
		$.ajax({
			type: "POST",
			url: "./assets/php/postComment.php",
			data: {f_name:f_name,f_website:f_website,f_comment:f_comment,f_vercode:f_vercode,f_parent:f_parent,f_code:f_code},
			error: function(msg){
				$('#form_notify').html('Connection error');
			},
			success: function(msg){
				
				if(msg.substr(0,7)!='Invalid'){
					$('#form_firstname').val('Name (required)');
					$('#form_website').val('Website (e.g. www.mywebsite.com)');
					$('#form_comment').val('Write your comment here');
					$('#form_vercode').val('');
					setTimeout("location.reload(true);",3000);
				}
				
				$('#form_notify').html(msg);
			}
		});
		
	}
   	
}

function showfeatured(prenext){
	var curimage = 0;
	var nextimage = 0;
	var allimages = $('.post_featured_pan img').length;
	$('.post_featured_pan img').each(function(index){
		if($(this).is(':visible')){
			curimage = index+1;
		}
	});
	//alert(curimage);
	if(prenext==1){
		nextimage=curimage-1;
		if(nextimage<1)nextimage=allimages;
	}else{
		nextimage=curimage+1;
		if(nextimage>allimages)nextimage=1;
	}
	$('#fim_'+curimage).hide();
	$('#flink_'+curimage).hide();

	$('#fim_'+nextimage).fadeIn('slow');
	var w_container = $('.post_featured_pan').width();
	var h_container = $('.post_featured_pan').height();
	var le = -Math.floor(($('#fim_'+nextimage).width()-w_container)/2);
	var to = -Math.floor(($('#fim_'+nextimage).height()-h_container)/2);	
	if(le>0)le*=-1;
	if(to>0)to*=-1;
	$('#fim_'+nextimage).css("left",le).css("top",to);
	$('#flink_'+nextimage).fadeIn('slow');
	initfeatured();
}
function initfeatured(){
	var curentimage = $('.post_featured_pan img:visible');
	var w_container = $('.post_featured_pan').width();
	var h_container = $('.post_featured_pan').height();
	var w_image = $(curentimage).width();
	var h_image = $(curentimage).height();
	var pos = $('.post_featured_pan').position();
	var x_container = pos.left;
	var y_container = pos.top;
	$('.post_featured_pan').unbind("mousemove").unbind("mouseenter").unbind("mouseleave");
	$('.post_featured_pan').mouseenter(function(e){
		window.clearInterval(inter);
		inter='';
	}).mouseleave(function(e){
		inter = self.setInterval( "showfeatured(2)", 10000 );
	}).bind("mousemove",function(e){
		var curimage = $('.post_featured_pan img:visible');
		var perx = Math.floor((e.pageX-x_container)/w_container*100)/100;
		var pery = Math.floor((e.pageY-y_container)/h_container*100)/100;
		var w_image = $(curentimage).width();
		var h_image = $(curentimage).height();
		var le = -Math.floor((w_image-w_container)*perx);
		var to = -Math.floor((h_image-h_container)*pery);
		var app = navigator.appVersion;
		if(navigator.appCodeName=="Mozilla" && app.search("Windows")!=-1 && app.search("Chrome")==-1){
			$(curentimage).dequeue().css({"left": le, "top":to });
		}else{
			$(curentimage).dequeue().animate({"left": le, "top":to },"slow");
		}
	});
}


function gensearch() { 
	$('#searchsub').click();
   	
}

