
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);
	}
}
var current_test = 0;
function switch_slidehosw_image(image_url) {
    $('.spot_box .slideshow_img:visible').hide();
    $('.spot_box .slideshow_img:hidden').attr('src', image_url);
    $('.spot_box img').show();	
}

var current_test = 0;

function switch_testemonial_text(old_test,new_test){
	$('#test__'+old_test).hide();
	$('#test__'+new_test).show();
}
/*
function switch_slidehosw_image_stopped(image_url) {
    $('.spot_box img:visible').hide('fast',  function() {
        $('.spot_box img:hidden').attr('src', image_url);
        setTimeout(function() {
            $('.spot_box img').show('fast');
        }, 1000);
    });
	
}
*/
var href_url = '';
function slideshow() {
	var old_test;
	var new_test;
	$.each(images, function(i, val) {
            // "data" is a JSON object where each cell holds the src of the image
			setTimeout(
				function() {
					if (i > 0 && (i%2) == 0) { 
						old_test = current_test;
						current_test++;
						if (current_test == test_counter) {
							current_test=0;
						}
						new_test = current_test;
						switch_testemonial_text(old_test,new_test);
					}
					switch_slidehosw_image(href_url+val);
					
					if (i == images.length-1) {
						setTimeout(function() {
							slideshow();
						},4000);
					}
					
				}, i*5000
			);
			
				
					
				
			
		}
    );
}

function testemonaial_handler() {
	var i = 0 ;
	while ( i <= test_counter) {
		var old_test = i;
		i++;
		if (i == test_counter) {
			i=0;
		}
		var new_test = i;
		setTimeout(
				function() {
				}, i*10000
			);
	}
}
function display_text(current_text_id) {
	$(' .current_displayed').hide();
	$(' .current_displayed').removeClass('current_displayed');
	$(current_text_id).show();
	$(current_text_id).addClass('current_displayed');
}
function set_selected(object){
	$(' .submenubar .selected').removeClass('selected');
	$(object).addClass('selected');
}
function handle_question(quest_id) { 
	if ( $('#question_'+quest_id).hasClass('opened') ) {
		$('#question_'+quest_id).removeClass('opened');
		$('#answer_'+quest_id).hide();
		$('#question_'+quest_id).addClass('closed');
	} else {
		$('#question_'+quest_id).removeClass('closed');
		$('#answer_'+quest_id).show();
		$('#question_'+quest_id).addClass('opened');
	}	
}

function get_children(href,parent_id,doc_type,div_id,selected_value){
	$('#'+div_id+' select').hide();
	$('#'+div_id+' .loader').show();
	$.get(href,{ 'action' : 'get_children','group_id' : parent_id,'doc_type': doc_type},
		function(data){
			$('#'+div_id+' select').html('<option value="">Select</option>');
			if (data != 'failed') {
				for (var i = 0; i < data.length; i++) {
					if (data[i]['id'] == selected_value) {
						$('#' + div_id + ' select').append('<option selected="selected" value="' + data[i]['id'] + '">' + data[i]['title'] + '</option>');
					}
					else {
						$('#' + div_id + ' select').append('<option value="' + data[i]['id'] + '">' + data[i]['title'] + '</option>');
					}
				}
			} else {
				$('#'+div_id+' select').append('<option value="">&nbsp;</option>');
			}
			$('#'+div_id+' .loader').hide();
			$('#'+div_id+' select').show();
			/*
			if (div_id == 'product_family_container') {
				get_children(href,$('#'+div_id+' select').val(),'product','product_container',);
			}
			*/ 
		}
	,'json');
}
function info_type_change_accessories(info_select_val, category_accessory_val, type_family_container_id, type_family_val) {
	//console.log('***\n info_select_val: ' + info_select_val + ';\n category_accessory_val: ' + category_accessory_val + ';\n type_family_container_id: ' + type_family_container_id + ';\n type_family_val: ' + type_family_val);
	if($('#info_type_select').val() == 'accessory' || info_select_val == 'accessory') {
		$('#category_container').hide();
		$('#category_container_accessory').show();
		$('#product_family_container').hide();
		$('#type_family_container').show();
		$('#product_container').hide();
		
		//"Type" pulldown is empty - creating its options by default - for Flame Detectors 
		//Accessory && Flame
		if (category_accessory_val == '172482' || category_accessory_val == '') {
			get_children(HREF,'172482','group',type_family_container_id,type_family_val);
			//$('#type_family_container').val(type_family_val);
		}
		//Accessory && Gas
		else {
			$('#category_accessory_select').val(category_accessory_val);
			get_children(HREF,category_accessory_val,'group',type_family_container_id,type_family_val);
		}
	}
	//Product
	else if($('#info_type_select').val() == 'product') {
		$('#category_container_accessory').hide();
		$('#category_container').show();
		$('#type_family_container').hide();
		$('#product_family_container').show();
		$('#product_container').show();
	}
	else {
		$('#category_select').html(info_type_change.categories_cont);
		get_children(HREF,'category_select','group','product_family_container');
		$('#category_container').show();
		$('#product_family_container').show();
		$('#product_container').show();
		$('#approval_separator').hide();
		$('#search_approvals_by').hide();
		$('#middle_approvals_pulldown').hide();
		$('#approvals_product_container').hide();
		$('#approvals_type').hide();
		$('#approvals_label').hide();
		$('#approvals_label_type').hide();
	}
}

function info_type_change() {
	if($('#info_type_select').val() == 'article_doc') {
		$('#category_select').html(
		'<option value="">All Categories</option>' +
		'<option value="military">Military</option>' +
		'<option value="industrial">Industrial</option>'		
		);
		
		$('#category_container').show();
		$('#product_family_container').hide();
		$('#product_container').hide();
		$('#approvals_label').hide();
		$('#approvals_label_type').hide();
	}
	else if($('#info_type_select').val() == 'presentations') {
		$('#category_select').html(
		'<option value="">All Categories</option>' +
		'<option value="military">Military</option>' +
		'<option value="industrial">Industrial</option>'		
		);
		
		$('#category_container').show();
		$('#product_family_container').hide();
		$('#product_container').hide();
		$('#approvals_label').hide();
		$('#approvals_label_type').hide();
	}
	else if($('#info_type_select').val() == 'videos') {
		$('#category_container').hide();
		$('#product_family_container').hide();
		$('#product_container').hide();
		$('#approval_separator').hide();
		$('#search_approvals_by').hide();
		$('#middle_approvals_pulldown').hide();
		$('#approvals_product_container').hide();
		$('#approvals_type').hide();
		$('#approvals_label').hide();
		$('#approvals_label_type').hide();
	}
	else if($('#info_type_select').val() == 'approvals') {
		$('#category_container').hide();
		$('#product_family_container').hide();
		$('#product_container').hide();
		$('.approval_separator').show();
		$('.info_search_separator').hide();
		$('#search_approvals_by').show();
		$('#middle_approvals_pulldown').show();
		$('#approvals_product_container').show();
		$('#approvals_type').show();
		$('#approvals_label').show();
		$('#approvals_label_type').show();
	}
	else {
		$('#category_select').html(info_type_change.categories_cont);
		get_children(HREF,'category_select','group','product_family_container');
		$('#category_container').show();
		$('#product_family_container').show();
		$('#product_container').show();
		$('#approval_separator').hide();
		$('#search_approvals_by').hide();
		$('#middle_approvals_pulldown').hide();
		$('#approvals_product_container').hide();
		$('#approvals_type').hide();
		$('#approvals_label').hide();
		$('#approvals_label_type').hide();
	}
}

function nature_of_call_change() {
	if($('#inquiry_nature').val() == strings.product_inquiry || $('#inquiry_nature').val() == strings.purchase_info || $('#inquiry_nature').val() == strings.general_inquiry)
	{
		$('#product_family_container').show();
		$('#product_container').show();
	}
	else {
		$('#product_family_container').hide();
		$('#product_container').hide();
	}
}

function make_search() {
	document.info_search.submit();
}

function search_goto(href) {	
	 if ($('#product_container select').val()) {
	 	window.location = href + $('#product_container select').val();
	 } else if ($('#product_family_container select').val()){
	 	window.location = href + $('#product_family_container select').val();
	 }
}

function sort_faqs(main_section) {
	$('.q_container:visible').hide();
	$('.'+main_section).show();	
	if (main_section == 'all') {
		$('.q_container').show();
	}
}

function get_series_products(href,group_id){
	$('#product_container .product_ids').remove();
	$('#product_container .loader').show();
	$.get(href,{ 'action' : 'get_series_products','group_id' : group_id},
		function(data){
			$('#product_container').append('<select class="product_ids"></select>');
			for (var i = 0;i < data.length;i++ ) {
				$('#product_container .product_ids').append('<option value="'+data[i]['id']+'">'+data[i]['title']+'</option>');
			}
			$('#product_container .product_ids').live('change',function(data){
				//get_models(href,$(this).val());
			});
		}
	,'json');
	
	
	$('#product_container .product_ids').show();
	$('#product_container .loader').hide();
}
function get_models(href,value){
	//alert(value);
}

function logg(val) {
	if(window.console) {
		console.log(val);
	}
}

function get_region_countries_b(region_id) {
	var num = region_id.substr(4);
	$('.region_countries').css('display', 'none');
	$('.region_countries').attr('name','');
	$('#'+region_id).show();
	$('#'+region_id).attr('name','region_country');
}



function set_search_type(search_type) {
		if (search_type == 'approval_type') {
			console.log('this is search_type: ' + search_type);
			$('.main_sections').hide();
			$('.approval_types_results').show();
		} else {
			$('.approval_types_results').hide();
			$('#main_sections_select').val('');
			$('.main_sections').show();
		}
		//get_children();
		
		$('#middle_approvals_pulldown select:visible').change();
	}

function get_groups_by_parameter(href, group_id, div_id, param_name) {
	//console.log('href:' + href + '\n' + 'group_id: ' + group_id + '\n div_id: ' +div_id + '\n parame_name: ' +param_name);
	$('#'+div_id+' select').hide();
	$('#'+div_id+' .loader').show();
	$.get(href,{ 'action' : 'get_groups_by_parameter','group_id' : group_id, 'param_name' : param_name},

		function(data){
			$('#'+div_id+' select').html('');
			if (data != 'failed') {
				$('#' + div_id + ' select').append('<option value="">Select</option>');
				for (var i = 0; i < data.length; i++) {
					$('#' + div_id + ' select').append('<option value="' + data[i]['id'] + '">' + data[i]['title'] + '</option>');
				}
			} else {
				//console.log('failed');
				//$('#'+div_id+' select').append('<option value="">&nbsp;</option>');
				$('#'+div_id+' select').append('<option value="">Select</option>');

			}
			$('#'+div_id+' .loader').hide();
			$('#'+div_id+' select').show();
			if (div_id == 'product_family_container') {
				get_children(href,$('#'+div_id+' select').val(),'product','approvals_product_container');
			}
		}
	,'json');
}

//On Download page:
//In case of: For pulldaown = Manuals and Category pulldown = Military, Series and Model pulldowns need to be hidden
function hide_series_and_model_pulldowns (for_select_val, category_select_val) {
	if (for_select_val == 'manuals' && category_select_val == '167499') {
		$('#product_family_container').hide();
		$('#product_container').hide();
	}
	else {
		$('#product_family_container').show();
		$('#product_container').show();
	}
}
function hide_series_and_model_pulldowns_accessories (search_by_select_val, category_select_val) {
	//console.log(search_by_select_val, category_select_val);
	if (search_by_select_val == 'accessory') {
		$('#product_family_container').hide();
		$('#product_container').hide();
	}
	else {
		$('#product_family_container').show();
		$('#product_container').show();
	}
}

