$.lazy({
	src:'/system/resources/js/tooltip/easyTooltip.js',
	name:'easyTooltip'
});

$.lazy({
	src:'/system/resources/js/jquery.equalheights.js',
	name:'equalHeights'
});

/*
$.lazy({
	src:'/system/resources/js/carousel/jquery.jcarousel.pack.js',
	name:'jcarousel',
	dependencies:{
		css:[
			'/system/resources/css/carousel/jquery.jcarousel.css',
			'/system/resources/css/carousel/skin.css',
			'/system/resources/css/carousel/carousel.css'
		]
	}
});
*/

$.lazy({
	src:'/system/resources/js/jquery.lightbox-0.5.js',
	name:'lightBox',
	dependencies:{
		css:['/system/resources/css/lightbox/style.css']
	}
});

$(function($) {
	if($('body#gallery').length > 0) {
		$('.layout-col').equalHeights(700);
	} else {
		$('.layout-col').equalHeights(400);
		$('.twocols .col').equalHeights(100);
	}
	
	$('a[rel=lightbox]').lightBox();
//	$(".show-tooltip").easyTooltip();
	
	if($('.content_products').length > 0) {
		get_products();
	}
	
	if($('#mycarousel').length > 0) {
		var url_first_video = $("#mycarousel").find("a").attr("id").replace('album-', '');
		
		$('#mycarousel').jcarousel({
			initCallback:function(carousel, state) {
				$('#mycarousel').find('.jcarousel-item img').css('z-index', 1);
			}
		});
	}
	
	if($('.content_albums').length > 0) {
		get_albums();
		get_videos();
	}
	
	if($('#flowplayer').length > 0) {
		var player = flowplayer("flowplayer", "/system/resources/player/flowplayer.commercial-3.1.5.swf",{
			key:'#$aaf0b38fd4d551fe149',
			clip:{ 
			  url:$('#flowplayer').attr('href'), 
			  autoPlay:$('#flowplayer').hasClass('autoplay'),
			  scaling:'scale'
			},
			canvas:{ 
				backgroundImage:'url(/system/resources/images/skin_video_player.jpg)',
				backgroundGradient:[0,0,0,0.1,0.4]
			},
			play:{ 
				  width:'50%', 
			      height:'50%',
				  top:0
		    }, 
			plugins:{ 
		        controls:{             
		            //background:'url(system/resources/images/skin_video_player.jpg) no-repeat', 
		            all:false, 
		            scrubber:true, 
		            play:true, 
					fullscreen:true,
		            mute:true, 
		             
		            // custom colors 
		            bufferColor:'#333333', 
		            progressColor:'#0099FF',             
		            buttonColor:'#0099FF', 
		            buttonOverColor:'#0033FF', 
		             
		            // tooltip background color (since 3.1.0) 
		            tooltipColor:'#0099FF', 
		             
		            // custom dimension 
		            height:20, 
		            width:400, 
		             
		            // setup auto hide 
		            autoHide:'always',             
		             
		            // a little more styling              
		            width:'98%',  
		            bottom:5, 
		            left:'50%', 
		            borderRadius:0
				}
			} 
		});
	}
	
//	set_events_album();
});


// after albums and videos loaded with ajax we need to fix page height
function media_height_fix()
{
	if($('.content_albums').length > 0)
	{
		$('#left_content').height($(".body_content_box").height() + 35);

		if($('#sidebar').height() < $('#left_content').height())
		{
			$('#sidebar').height($('#left_content').height());
		}
		else
		{
			$('#left_content').height($('#sidebar').height());
		}
	}
}


function get_albums()
{
	if($('.content_albums').attr('id') !== '') {
		pagination_albums();
		set_events_albums();
		return;
	}
	
    $.ajax({
		url:"/gallery/albums",
		type:"post",
		data:{ajax:true},
		async:true,
		success:function(res) {
			$(".content_albums").html(res);
            pagination_albums();
            set_events_albums();

            media_height_fix();
		}
    });
}


function get_videos()
{
	$.ajax({
		url:"/gallery/videos",
		type:"post",
		async:true,
		data:{ajax:true},
		success:function(res) {
			$(".content_videos").html(res);
			$('a[rel=lightbox]').lightBox();
            pagination_videos();

            media_height_fix();

		}
	});
}


function get_products(start_from) {
	if(start_from === undefined)
		start_from = 0;

	$.ajax({
		url:"/products/product/" + $('.content_products').attr('id').replace('category-', '') + '/' + start_from,
		type:"post",
		data:{ajax:true},
		async:true,
		beforeSend:function() { 
			$(".content_products").find(".loading_products").show();
		},
		success:function(res) {
			$(".content_products").html(res);
			pagination_products();
			$('a.modal').lightBox();
			$(".content_products").find(".loading_products").hide();
		}
	});
}


function pagination_albums() {
	$(".content_albums .pagination_content").find("a").unbind().click(function(event) {
		event.stopPropagation();
		
		$.ajax({
			url:$(this).attr("href"),
			type:"post",
			data:{ajax:true},
			async:true,
			beforeSend:function() { 
				$(".content_albums").find(".loading_album").show();
			},
			success:function(res) {
	            $(".content_albums").hide();
				$(".content_albums").html(res);
				pagination_albums();
	            set_events_albums();
	            $('a[rel=lightbox]').lightBox();
				$(".content_albums").find(".loading_album").hide();
	            $(".content_albums").fadeIn();
			}
		});
		
		return false;
	});
}

function pagination_photos() {
	$(".content_photos .pagination_content").find("a").unbind().click(function(event) {
		event.stopPropagation();
		
		$.ajax({
			url:$(this).attr("href"),
			type:"post",
			async:true,
			data:{ajax:true},
			beforeSend:function() { 
				$(".content_photos").find(".loading_photos").show();
			},
			success:function(res) {
				$(".content_photos").hide();
				$(".content_photos").html(res);
				pagination_photos();
	            set_gotoalbums();
				$('a[rel=lightbox]').lightBox();
				$(".content_photos").find(".loading_photos").hide();
	            $(".content_photos").fadeIn();
			}
		});
		
		return false;
	});
}
	
function pagination_products() {
	$(".content_products .pagination_content").find("a").unbind().click(function(event) {
		event.stopPropagation();
		
		$.ajax({
			url:$(this).attr('href'),
			type:"post",
            data:{category_id:$(this).attr('id').replace('product-', ''), ajax:true},
			beforeSend:function() { 
				$(".content_products").find(".loading_products").show();
			},
			async:true,
			success:function(res) {
				$(".content_products").html(res);
				$(".content_products").find(".loading_products").hide();
				pagination_products();
				$('a[rel=lightbox]').lightBox();
			}
		});
		
		return false;
	});
}

function pagination_videos() {
	$(".content_videos .pagination_content").find("a").unbind().click(function(event) {
		event.stopPropagation();
		var url_to = $(this).attr("href");
		if(url_to) {
			$.ajax({
				url:url_to,
				type:"post",
				data:{ajax:true},
				beforeSend:function() { 
					$(".content_videos").find(".loading_videos").show();
				},
				async:true,
				success:function(res) {
		            $(".content_videos").hide();
					$(".content_videos").html(res);
					pagination_videos();
					$('a[rel=lightbox]').lightBox();
					$(".content_videos").find(".loading_videos").hide();
		            $(".content_videos").fadeIn();
				}
			});
		}
		
		return false;
	});
}

function set_events_albums() {
    $(".content_albums .gallery").find("a").unbind().click(function(event) {
		event.stopPropagation();
		
		var name_album = $(this).attr("title");
		$.ajax({
			url:$(this).attr('href'),
			type:"post",
			data:{ajax:true},
			async:true,
			beforeSend:function() { 
				$(".content_albums").find(".loading_album").show();
			},
			success:function(res) {
				//$(".loading_icon").remove();
	            $(".content_albums").hide();
				$(".content_albums").html(res);
	            $(".content_albums").addClass("content_photos");
	            $("#headeralbums").html('"'+name_album+'"'+" album");
	            //$(".content_photos").removeClass("content_albums");
				pagination_photos();
	            set_gotoalbums();
				$('a[rel=lightbox]').lightBox();
				$(".content_photos").find(".loading_photos").hide();
	            $(".content_albums").fadeIn();

	            media_height_fix();
			}
		});
		
		return false;
	});
}

function set_gotoalbums() {
    $(".gotoalbums a").unbind().click(function(event){
        event.stopPropagation();
        $.ajax({
			url:"/gallery/albums",
			type:"post",
			data:{ajax:true},
			beforeSend:function() { 
				$(".content_photos").find(".loading_photos").show();
			},
			async:true,
			success:function(res) {
			    $(".content_albums").hide();
				$(".content_albums").html(res);
	            $("#headeralbums").html("PHOTO ALBUMS");
				pagination_albums();
	            set_events_albums();
				$(".content_albums").find(".loading_album").hide();
	            $(".content_albums").fadeIn();
			}
        });
        
        return false;
    });
}

/* functionality to play video from carousel images url*/
function updateContents( url, event_id ) {
	updateVideo(url);
}

function updateVideo(url) {
	var controlsP = $f('player').getPlugin("controls");
	controlsP.css('display', 'block');
	//controls_plugin.update({play:true;});
	$f('player').play(url);
}
