var target;
var ajaxUrl = url + "ajax/";
var pageNumber = p;

$(document).ready(function(){
    var options = { 
        dataType:	'json',
        url:		ajaxUrl + 'submit',
        success:    ajaxFormResponse,
    }; 

    $('.ajax-form').ajaxForm(options);
    
    
	$('.confirm').click(function(){
		return confirm($(this).attr("title"));
	});

	$('#crop').Jcrop({
		setSelect: [0,0,250,250],
		onChange: setCoords,
		onSelect: setCoords,
		aspectRatio: 1
	});	
	
    $('a[rel*=facebox]').facebox({
        loadingImage : css + 'facebox/loading.gif',
        closeImage   : css + 'facebox/closelabel.gif'
      }); 

	var mouseX = 0;
	var mouseY = 0;
	$().mousemove( function(e) {
	   mouseX = e.pageX; 
	   mouseY = e.pageY;
	 });	

	var jcVisible = 10;
	
	$('#fakeli').remove();
	$('#mycarousel').jcarousel({
        // Configuration goes here
        scroll:10,
        visible:jcVisible,
        size: $('#hiddenCatCount').attr('value'),
        start:(jcVisible * (pageNumber-1))+1,
        itemLoadCallback: {
			onAfterAnimation: cuteCarouselLoadCallback
		}
    });
    
    $('.prev a img').animate({opacity:0.1}, 10);
    $('.next a img').animate({opacity:0.1}, 10); 

	$('.prev').hover(
		function(){
			$('.prev a img').animate({opacity:0.5}, 'fast');
		}, 
		function(){
			$('.prev a img').animate({opacity:0.1}, 'fast');
		}
	);

	$('.next').hover(
		function(){
			$('.next a img').animate({opacity:0.5}, 'fast');
		}, 
		function(){
			$('.next a img').animate({opacity:0.1}, 'fast');
		}
	);	
	
	$('#share-link-input').focus(function(){$(this).select()});

	$('#cute-fast-mod').change(function(){
		var values = {};
		var str = '';	
		$('#cute-fast-mod').find("input[type='hidden'], input[checked], option[selected]").each(function(){
			if (this.disabled != 'disabled' || this.type == 'hidden'){
				values[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
				str = str + ' ' + this.name + '(' + this.parentNode.name + '):' + this.value + "\n";
			}
		});
		if (values['status'] != 0 && values['category'] != 0){
			$('#cute-fast-mod').ajaxSubmit(options);
		}
	});
});

function ajaxFormResponse(res, status){
	if (res.callback){
		window[res.callback](res);
	}
	return false;

}

function commentCallback(res){
	if (res.result == 1){
		$( "form" )[ 0 ].reset();
		var newdiv = $("<div></div>").addClass('just-posted').html(res.data);
		$(res.target).before(newdiv);
		$(newdiv).fadeIn('slow');
	}

	if (res.messages){
		if ($("#result").text() != ''){
		$("#result")
		.animate({"opacity": 0}, 'slow', function(){
		$("#result").html(res.messages)
		.animate({"opacity": 1}, 'slow')
		});
	} else {
		$("#result").css('opacity', 0) 
		.html(res.messages)
		.animate({"opacity": 1}, 'slow');
	}
	}	
}


var firstCallback = 1;
function cuteCarouselLoadCallback(carousel, state){
	var req = 0;

   	if (firstCallback == 1){
    	cuteAddCarouselItems(carousel, carousel.first, carousel.last, images);
    	firstCallback = 0;
    	images = Array();
    	return ;
   	}

	for (var i = carousel.first; i <= carousel.last; i++) {
        if (!carousel.has(i)) req = 1;
    }	

    if (req == 1){
		var call = ajaxUrl+'call/?act=' + 'cute_images_carousel_add';	
   		$.getJSON(call, {first: carousel.first, last: carousel.last, category: $('#hiddenCat').attr('value')}, 
   			function(data){ 
   				cuteAddCarouselItems(carousel, carousel.first, carousel.last, data);
   			}
   		);
   	}
}

function cuteAddCarouselItems(carousel, first, last, data){
	var i, c, x, b;	//    	alert(data.items[i]);
	c = 0; b = 0;
    for (i in data.items) {
		x = first + c;
        if (!carousel.has(x)) {
            carousel.add(x, data.items[i]);
            b++;
        }
        c++;	
    }	
}

function setCoords(c){
	$('#x1').val(c.x);
	$('#y1').val(c.y);
	$('#x2').val(c.x2);
	$('#y2').val(c.y2);
	$('#w').val(c.w);
	$('#h').val(c.h);
	if (c.w < 100 || c.h < 100){
		$('#crop-button').attr("disabled", true);
	} else {
		$('#crop-button').removeAttr("disabled");
	}
};

function jAction(vars, el){
	var call = ajaxUrl+'call/?act=' + vars.act;
	
	if (el.value) vars.data.sender_value = el.value;
	if (el.id) vars.data.sender_id = el.id;	

	if (vars.toggle) $(vars.toggle).toggle();
	if (vars.toggleClass) $(el).toggleClass(vars.toggleClass);		
	if (vars.data.target == undefined) vars.data.target = $(el).attr('id');
		
	$.getJSON(call, vars.data, window[vars.callback]);	
	return false;
}

function ajax_submit(form_id){
	var params = {};
	$('#'+form_id).find("input[checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='submit'], option[selected], textarea")
	.each(function() {
	if (this.disabled != 'disabled' || this.type == 'hidden'){
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		}
	});
	$.post(ajaxUrl+'submit/', params, callback, "html");
	return false;
}

function insideCallback(data, textStatus){
		$(target).html(data);
}

function beforeCallback(data, textStatus){
	if (data != 'null'){
		$(target).before(data);
	}
}

function callback(data, textStatus) {
	if (data != 'null'){
		$("#result").html(data);
	}
}

function insertHTML(target, method, html){
	$(target).append(html);
}
