//
Liferay.ext.getbgimgid = function(card) {
	var $ = jQuery;
	return ($("div.testimonial_container", card).attr("id").replace(/tm_/,"bg_")).toLowerCase();
};

//Caches the next background image in
Liferay.ext.cachenextbg = function(card, bg_next) {
	var $ = jQuery;
	var img_next = Liferay.ext.getbgimgid(card);//name of the next bg image eg "bg_dtco"
	bg_next.css(
		"backgroundImage",
		bg_next.css("backgroundImage").replace(/bg_[^.]+/, img_next)
	);
};

//Triggers the next queue item
Liferay.ext.dequeue_bg = function() {

	$("#wrapper").dequeue("bg");
	
};

//
Liferay.ext.displayTestimonial = function(obj_out, obj_in, obj_next, i) {
	var $ = jQuery;
	var dt = 1500;
	
	//img container to show, for img case
	var bg_in = $("#flashbg_object_" + i);

	if (Liferay.ext.noflash)
	//forcing to show the selected bg
		Liferay.ext.cachenextbg(obj_in, bg_in);

	//dummy
	if (!!obj_out)
	$("#wrapper").queue("bg", function() {});

	//1. hiding testimonial obj_out if there is
	if (!!obj_out)
	$("#wrapper").queue("bg", function() {
		Liferay.ext.displayBgMenuItem(Liferay.ext.index_testimonial_prev, false, Liferay.ext.dequeue_bg );
		$(obj_out).fadeOut(dt, function(){});
	});

	//2. starting bg transition
	$("#wrapper").queue("bg", function(){

		try{
			if (Liferay.ext.noflash) {

				//img case
				bg_in.fadeIn(dt, function(){});
				
				if (Liferay.ext.bg_out != "undefined" && Liferay.ext.bg_out)
					Liferay.ext.bg_out.fadeOut(dt, function(e){});
			
			} else {
				
				//flash case
				var flash_par = ("" + $("div.testimonial_container", obj_in).attr("id")).substr(3).toUpperCase();
				Liferay.ext.flashbg.gotoBG(flash_par);//HOME,DTCO,DTMA,TRNG
						
			}
		}catch(err){
		//alert("somethig weng wong");
		}

		setTimeout("Liferay.ext.dequeue_bg()", dt);
	});

	//3. displaying testimonial obj_in
	$("#wrapper").queue("bg", function(){

		$(obj_in).fadeIn(dt, function(){
			
			if (Liferay.ext.noflash) {
				Liferay.ext.cachenextbg(obj_next, $("#flashbg_object_" + (1-i)) );
				Liferay.ext.bg_out = bg_in;
			}
		
		});

		Liferay.ext.displayBgMenuItem(Liferay.ext.index_testimonial, true, function(){
	
			Liferay.ext.dequeue_bg();
			Liferay.ext.index_testimonial_prev = Liferay.ext.index_testimonial;
			Liferay.ext.animating_bg = false;
		
		});
	});
	


	Liferay.ext.dequeue_bg();
};
















Liferay.ext.index_testimonial_prev = -1;
Liferay.ext.index_testimonial = -1;
Liferay.ext.flip = 1;
Liferay.ext.animating_bg = false;
Liferay.ext.quicklink_timer = null;
Liferay.ext.bgMenu = null;




Liferay.ext.jumpToBg = function(i) {

	if (Liferay.ext.animating_bg || ((i+1)==Liferay.ext.index_testimonial))
		return;

	if (Liferay.ext.quicklink_timer)
		clearTimeout(Liferay.ext.quicklink_timer);
	Liferay.ext.quicklink_timer = null;
	
	Liferay.ext.index_testimonial = i;
	Liferay.ext.animateTestimonials();
};

//Displays/hides the given minimenu item
Liferay.ext.displayBgMenuItem = function(i, bdisplay, cb) {

	$("#minimeni-item" + i + " a").animate({
		opacity : (bdisplay ? 1 : 0.4)
	}, 1500, "linear", cb);
	
};

//Creates the mini menu
Liferay.ext.createBgMenu = function(cards) {
	var $ = jQuery;
	
	var ul = Liferay.ext.bgMenu = $("<ul>");
	
	cards.each(function(i){

		ul.append( $("<li>").attr({
			id : "minimeni-item" + i
		}).append( $("<a/>").attr({
			href : "javascript:Liferay.ext.jumpToBg(" + (i-1) + ")"
		}) ));
		
	});
	
	$("#content-wrapper").append( $("<div id='minimenu'>").append(ul) );

};



Liferay.ext.animateTestimonials = function() {
	
	var $ = jQuery;

	var cards = null, ready = true;
	try {
		cards = $("#list_testimonials ul.articles > li");//array of LI
		if (cards.size() < 5)
			ready = false;
	}catch(e) {
		ready = false;
	}

	if (!ready) {
		setTimeout(Liferay.ext.animateTestimonials, 100);
		return;
	}
	


	//creating background menu
	if (!Liferay.ext.bgMenu)
		Liferay.ext.createBgMenu(cards);

	//incrementing index
	Liferay.ext.index_testimonial++;
	if (Liferay.ext.index_testimonial >= cards.size())
		Liferay.ext.index_testimonial = 0;

	//to hide in this round:
	var i1 = (Liferay.ext.index_testimonial_prev >= 0 ? Liferay.ext.index_testimonial_prev : Liferay.ext.index_testimonial-1);
	
	//to show in this round:
	var i2 = Liferay.ext.index_testimonial;
	
	//to show in NEXT round
	var nexti = i2+1;
	if (i2>=cards.size()) nexti = 0;

	Liferay.ext.flip = 1-Liferay.ext.flip;

	//get flash object (not neccessary exists)
	Liferay.ext.flashbg = swfobject.getObjectById("flashbg_object");

	//choose display mode (flash or html)
	Liferay.ext.animating_bg = true;
	Liferay.ext.displayTestimonial( i1>=0?cards.get(i1):null, cards.get(i2), cards.get(nexti), Liferay.ext.flip);

	Liferay.ext.quicklink_timer = setTimeout(Liferay.ext.animateTestimonials, 10000);
};

jQuery(document).ready(function(){
	jQuery("div.testim").click(function() {
		window.location = jQuery("a", this).attr("href");
	});
});


