// JavaScript Document
jQuery(document).ready(function(){
	
/* jQUERY goes here  */
		
	jQuery('.open_panel').click(function(e){
		e.preventDefault();
		jQuery('.active_panel').fadeOut('slow');
		jQuery('.active_panel').removeClass('open_panel');
		var post_id = jQuery(this).get(0).rel;
		//console.log(post_id);
		jQuery('#panel' +post_id).fadeIn('slow');
		jQuery('#panel' +post_id).addClass('active_panel');
	});
	
	jQuery('.close_panel').click(function(e){
		e.preventDefault();
		//console.log('dupa');
		jQuery(this).parent().parent().parent().fadeOut('slow');
	});
	
	
	jQuery('.postbody img').each(function(){
		var height = jQuery(this).height();
		var width = jQuery(this).width();
		var correctWidth = 590;
		if (width > correctWidth){
			var ratio = correctWidth / width;
			var correctHeight = Math.round(height * ratio);
			//console.log(ratio +'old ' + width + ' ' + height + 'new ' + correctWidth + ' ' + correctHeight);
			jQuery(this).css({'width':correctWidth, 'height':correctHeight});
		}
	});
	
	var resize = setInterval(imageResize, 300);
	
	
	
});

function imageResize(){
	jQuery('.postbody img').each(function(){
			var height = jQuery(this).height();
			var width = jQuery(this).width();
			var correctWidth = 590;
			if (width > correctWidth){
				var ratio = correctWidth / width;
				var correctHeight = Math.round(height * ratio);
				//console.log(ratio +'old ' + width + ' ' + height + 'new ' + correctWidth + ' ' + correctHeight);
				jQuery(this).css({'width':correctWidth, 'height':correctHeight});
			}
		});
	
	jQuery('.avatar img').each(function(){
			var height = jQuery(this).height();
			var width = jQuery(this).width();
			var correctWidth = 130;
			if (width > correctWidth){
				var ratio = correctWidth / width;
				var correctHeight = Math.round(height * ratio);
				//console.log(ratio +'old ' + width + ' ' + height + 'new ' + correctWidth + ' ' + correctHeight);
				jQuery(this).css({'width':correctWidth, 'height':correctHeight});
			}
		});
}

// input field text reset functions
function resetField(field, value){
	if (field.value==value){
		field.value="";	
		return false;
	}
}

function startValue(field, text){
			if (field.value==""){
			field.value=text;	
			return false;
			}
}



/* ====== Ajax cart functions ====== */

function ajaxCartAddProduct () {
  var form = jQuery('#product_addtocart_form');
	var container = jQuery('#ajax_cart_container');
	var loader = jQuery('#ajax_cart_loading');
	var content = jQuery('#ajax_cart_content')
	var inputs = jQuery(':input', form).serialize();

    jQuery('html,body').animate({scrollTop: 0}, 200);

	container.show(1, function () {
		content.hide();
		loader.fadeIn('slow');
	});

	jQuery.post(form.get(0).action+"?isAjax=1", inputs, function (data, textStatus) {
		content.html(data);
		loader.fadeOut('slow', function () {
			content.fadeIn('slow', function() {
				ajaxCartSetTimeout(5000);
			});
		});
	}, "html");

	return false;
}

function ajaxCartRemoveProduct(a, removeUrl) {
  var message = jQuery('.ajax_cart_message');
  var prodName = jQuery(a).parent().siblings('.name').text();

	jQuery(a).parents('.product_row').fadeOut('slow', function () {
    message.removeClass('bad');
    message.text('Removed ' + prodName);
	});
	jQuery.get(removeUrl+'?isAjax=1');
	return false;
}

function ajaxCartSetTimeout(t) {
   window.ajaxCartTimeout=setTimeout("jQuery('#ajax_cart_container').fadeOut('slow');", t);
}

function ajaxCartClearTimeout() {
  if(typeof(window.ajaxCartTimeout)!='undefined'){
    clearTimeout(window.ajaxCartTimeout);
    window.ajaxCartTimeout=null;
  }
}

