
function switch_article_showcase_image(image_url) {
	$('#article .showcase .images .image img:visible').hide('slide', {direction:'right'}, 500, function() {
		$('#article .showcase .images .image img').attr('src', image_url);
		setTimeout(function() {
			$('#article .showcase .images .image img:hidden').show('slide', {direction:'right'}, 500);
		}, 500);
	});
}

function switch_article_showcase_thumb(mode, index, image_url, youtube_code) {
	$('#'+mode+'__thumb_'+index+':visible').hide('slide', {direction:'right'}, 200, function() {
		$('#'+mode+'__thumb_'+index).attr('src', image_url);
		setTimeout(function() {
			$('#'+mode+'__thumb_'+index+':hidden').show('slide', {direction:'right'}, 200);
			$('#'+mode+'__thumb_'+index).unbind('click');
			if (mode == 'images' && !/spacer_gray/.test(image_url)) {
				$('#'+mode+'__thumb_'+index).bind('click', function() {
					switch_article_showcase_image(image_url);
				});
			}
			if (mode == 'videos' && !/spacer_gray/.test(image_url)) {
				$('#'+mode+'__thumb_'+index).bind('click', function() {
					switch_article_showcase_video(youtube_code);
				});
			}
		}, 100);
	});
}

function switch_article_showcase_video(youtube_code) {
	//$('#article .showcase .videos .video:visible').hide('slide', {direction:'right'}, 500, function() {
		$('#article_showcase_youtube').html('');
		$('#article_showcase_youtube').flash(
	        { src:'http://www.youtube.com/v/'+youtube_code, width:398, height:290 },
	        { version:8 }
	    );
		//setTimeout(function() {
		//	$('#article .showcase .videos .video:hidden').show('slide', {direction:'right'}, 500);
		//}, 200);
	//});
}

function switch_article_showcase_file(file_url, file_name) {
	$('#'+mode+'__file_'+index+':visible').hide('slide', {direction:'right'}, 200, function() {
		$('#'+mode+'__file_'+index).html('<div class="file"><a href="'+file_url+'" target="_blank">'+file_name+'</a></div>');
		setTimeout(function() {
			$('#'+mode+'__file_'+index+':hidden').show('slide', {direction:'right'}, 200);
		}, 100);
	});
}




function article_showcase_page(href_url, mode, doc_id, lang_prefix, page_number, max_page) {
	
	if (mode == 'images') {
		if (page_number > 0 && page_number <= max_page) {
			
			if (page_number <= 1) {
				//$('.arrow_right').addClass('disabled_by_opacity');
				$('.arrow_right').addClass('disabled');
			} else {
				//$('.arrow_right').removeClass('disabled_by_opacity');
				$('.arrow_right').removeClass('disabled');
			}
			if (page_number >= max_page) {
				//$('.arrow_left').addClass('disabled_by_opacity');
				$('.arrow_left').addClass('disabled');
			} else {
				//$('.arrow_left').removeClass('disabled_by_opacity');
				$('.arrow_left').removeClass('disabled');
			}
			
			current_page = page_number;
			
			$.get(href_url, {'action':'get-article-showcase-media', 'mode':mode, 'doc-id':doc_id, 'lang-prefix':lang_prefix, 'page':page_number, 'limit':limit},
				function(data) {
					$.each(data, function(i, val) {
							// "data" is a JSON object where each cell holds the src of the image
							if (i == 0) {
								switch_article_showcase_image(href_url+val);
							}
							setTimeout(
								function() {
									//$('#debug').html('Index: '+i+' Val: '+val);
									switch_article_showcase_thumb(mode, i, href_url+val, '');
								}, i*100
							);
						}
					);
				},
				'json'
			);
		}
	}
	
	if (mode == 'videos') {
		if (page_number > 0 && page_number <= max_page) {
			
			if (page_number <= 1) {
				//$('.arrow_right').addClass('disabled_by_opacity');
				$('.arrow_right').addClass('disabled');
			} else {
				//$('.arrow_right').removeClass('disabled_by_opacity');
				$('.arrow_right').removeClass('disabled');
			}
			if (page_number >= max_page) {
				//$('.arrow_left').addClass('disabled_by_opacity');
				$('.arrow_left').addClass('disabled');
			} else {
				//$('.arrow_left').removeClass('disabled_by_opacity');
				$('.arrow_left').removeClass('disabled');
			}
			
			current_page = page_number;
			
			$.get(href_url, {'action':'get-article-showcase-media', 'mode':mode, 'doc-id':doc_id, 'lang-prefix':lang_prefix, 'page':page_number, 'limit':limit},
				function(data) {
					$.each(data, function(i, val) {
							if (i == 0) {
								switch_article_showcase_video(val.code);
							}
							setTimeout(
								function() {
									//$('#debug').html('Index: '+i+' Val: '+val);
									if (/spacer_gray/.test(val.thumb)) {
										switch_article_showcase_thumb(mode, i, href_url+val.thumb, '');
									} else {
										switch_article_showcase_thumb(mode, i, val.thumb, val.code);
									}
								}, i*100
							);
						}
					);
				},
				'json'
			);
		}
	}
	
	if (mode == 'audios') {
		if (page_number > 0 && page_number <= max_page) {
			
			if (page_number <= 1) {
				//$('.arrow_right').addClass('disabled_by_opacity');
				$('.arrow_right').addClass('disabled');
			} else {
				//$('.arrow_right').removeClass('disabled_by_opacity');
				$('.arrow_right').removeClass('disabled');
			}
			if (page_number >= max_page) {
				//$('.arrow_left').addClass('disabled_by_opacity');
				$('.arrow_left').addClass('disabled');
			} else {
				//$('.arrow_left').removeClass('disabled_by_opacity');
				$('.arrow_left').removeClass('disabled');
			}
			
			current_page = page_number;
			
			$.get(href_url, {'action':'get-article-showcase-media', 'mode':mode, 'doc-id':doc_id, 'lang-prefix':lang_prefix, 'page':page_number, 'limit':limit},
				function(data) {
					$.each(data, function(i, val) {
							setTimeout(
								function() {
									switch_article_showcase_file(mode, i, val);
								}, i*100
							);
						}
					);
				},
				'json'
			);
			
		}	
	}
	
	
}

function article_showcase_media(href_url, mode, doc_id, lang_prefix, page_number, max_page_local) {
	$('#article_showcase_youtube').html('');
	$('#article .showcase .button').removeClass('selected');
	if (current_mode != mode) {
		if (current_mode) {
			$('#article .showcase .'+current_mode+':visible').hide('slide', {direction:'up'}, 500);
		}
		setTimeout(function () {
			$('#article .showcase .'+mode+':hidden').show('slide', {direction:'up'}, 500, function() {
				article_showcase_page(href_url, mode, doc_id, lang_prefix, page_number, max_page_local);
				max_page = max_page_local;
				current_mode = mode;
				current_page = page_number;
				$('#button__'+mode).addClass('selected');
			});
		}, 500);
	}
}

function generate_discounts(number_of_items, doc_id, group_id, sub_group_id, update_cache) {
	$.get('/'+doc_id, {action:'generate_discounts', number_of_items:number_of_items, group_id:group_id, sub_group_id:sub_group_id, update_cache:update_cache}, function(data) {
		//console.log(data);
		$('#group_table #discounts .inner_container').html(data);
	}, 'html');
}

/* ## */

$(document).ready(function() {
	$('form').attr('autocomplete','off');
	$('.conference_search input').keyup(function (e) {
		if(e.keyCode == 13) {
			$('#conference_search_form').submit();
		}
	});
	$('.conference_search select').keyup(function (e) {
		if(e.keyCode == 13) {
			$('#conference_search_form').submit();
		}
	});
	$('#sidebar .categories a:last').addClass('last');	
});

