
$(function () { productClassification.initiate(); });

var productClassification = function () {

	var config = {
		//-- common --//
		  active : 'active'
		, option : 'a.nav_option'
		, classifications : '#snippet_classifications'
		, classCat : 'div.classification_category'
		, classItem : 'div.classification_item'
		, classificationItem : 'classification_item'
		//-- news & press --//
		, showAllButton : 'a#news_press_show_all'
		, showAllButtonContainer : 'div#news_press_button_container'
		, alltopics : '#news_press_item'

		//-- photo library --//
		, phLibContainer : 'div#phlib_categories'
		, phLibCategory : 'div.phlib_category_container'
		, showAllPhLibButton : '#phlib_show_all'
			
		//-- case study --//
		, csItems: 'div.cs_classification_item'//classification_item
		, showAllCsButton : '#cs_show_all'
		, showAllCsButtonContainer:	'div#button_container'
		, csClassification_item:'cs_classification_item'
		

		//-- collapsable navigation --//
		, open : 'open'
		, toggleHeader : '.snippet_header_toggle'
		, toggleNav : '.snippet_nav_toggle'

		//-- div name --//
		, promotionFilterHeader : '#buy_promo_filter_header h3.left'

	},

	params = {
		  categories : false
		, photoLibraryPage : false
		//for case studies page start here
		, caseStudiesPage : false
		, showAllCsButtonClicked:false
		,caseStudyToShow:3
		//for case studies page until here
		, first : true
		, speed : 500
	},

	priv = {

		setActiveClassification : function (id) {
			$id = $('#' + id);
			$(config.option).filter('.'+config.active).removeClass(config.active);
			$id.addClass(config.active);

			if (params.categories) {
				//document.title = "News and press - Latest News - " + $id.html() + " - Canon ${countryNameTitle}";
			};
		},

		animateClassificationContent : function (id) {
			if (params.categories && params.first) {
				$(config.showAllButtonContainer).fadeOut(params.speed);
				params.first = false;
			} else {
				if(id != 'classification_item' && !params.first)
					$(config.showAllButtonContainer).fadeIn(params.speed);
				else 
					$(config.showAllButtonContainer).fadeOut(params.speed);
			};
			
			if (!id) {
				$(config.classifications).animate( {opacity : 0} ,params.speed, function () {
					if (params.categories){
						$(config.classCat).show();
					};
					$(config.classifications).animate( {opacity : 1}, params.speed);
				});
			} else {
				var currentItems = '.' + id;
				$(config.classifications).fadeOut(params.speed, function (){
					if (params.categories){
						$(config.classCat).find(config.classItem).andSelf().hide();
						$(currentItems).show().parent().show(1, function (){
							$(config.classifications).fadeIn(params.speed);
						});

					} 
					 else if (params.caseStudiesPage ){
						// if (id != config.csClassification_item || params.showAllCsButtonClicked){
						 if (id != config.csClassification_item){
							 $(config.csItems).hide();
							 $(this).find(currentItems).show( 1, function () {
								$(config.classifications).fadeIn( params.speed );
							 });
							 $(config.showAllCsButtonContainer).hide();
							 
						 }
						 else{
							 $(config.csItems).hide();
							 $(config.classifications).fadeIn( params.speed );
							 $(this).find(currentItems).each(function (key,value) {
								 	if (key<params.caseStudyToShow)
								 	{	
								 		$(value).show(1);
								 		$(config.showAllCsButtonContainer).show(1);
								 	}
								});
						 }
					 }
					else {
						$(config.classItem).hide();
						$(this).find(currentItems).show( 1, function () {
							if(id != config.classificationItem){
								$id = $('#' + id);
								$(config.promotionFilterHeader).each(function () {
									if(!this.defaultVal){
										this.defaultVal = $(this).html();
									}
								});
								$(config.promotionFilterHeader).html($id.html());
							} else {
								$(config.promotionFilterHeader).each(function () {
									$(this).html(this.defaultVal);
								});
							}
							$(config.classifications).fadeIn( params.speed );
						});
					};
				});
			};
			
		},

		sortClassifications : function () {
			$(config.option).click( function () {
				priv.setActiveClassification(this.id);
				priv.animateClassificationContent(this.id);
			});

			//currently only used on the press releases page
			if (params.categories) {
				$(config.showAllButton).click( function () {
					priv.setActiveClassification('classification_item');
					priv.animateClassificationContent('classification_item');
					// $(config.alltopics).addClass(config.active);
				});
			};
		},

		getClassificationFromUrl : function (togglenav) {

			if (location.hash) {
				var param = location.hash.split('=');
				if (param[1]){

					priv.setActiveClassification(param[1]);
					priv.animateClassificationContent(param[1]);

					if (togglenav) {
						$('#'+param[1]).parent().prev().click();
					};
				};
			};
		},

		showAllPhLibItems : function () {
			var $items = $(config.phLibContainer).find(config.phLibCategory),
				$hidden = $items.filter(':hidden');

			$(config.showAllPhLibButton).click( function () {
				$(this).parent().fadeOut(params.speed, function () {
					$hidden.slideDown(params.speed);
				});
			});
		},
		
		showAllCsItems : function () {
			var $items = $(config.csItems),
				$hidden = $items.filter(':hidden');

			$(config.showAllCsButton).click( function () {
				$(this).parent().fadeOut(params.speed, function () {
					$hidden.slideDown(params.speed);
				});
				params.showAllCsButtonClicked=true;
			});
		},

		toggleNavOptions : function () {
			var baseUrl = main.getBaseUrl();
			$(config.toggleHeader).click( function () {

				if ( !$(this).hasClass(config.open) ) {

					$('.'+config.open).find('> h3').css({ backgroundImage : 'url(http://media.canon-asia.com/v3.5media/common/bt_arrow_right_small.gif)'});
					$('.'+config.open).removeClass(config.open).next().slideUp();

					$(this).find('> h3').css({ backgroundImage : 'url(http://media.canon-asia.com/v3.5media/common/bt_arrow_down_small.gif)'});
					$(this).addClass(config.open).next().slideDown();
				};
			});

			if (location.hash) {
				priv.getClassificationFromUrl(true);
			} else {
				$(config.toggleHeader).eq(0).click();
				$(config.toggleNav).eq(0).find(config.option+':eq(0)').click();
			};

		},

		compareOptionText : function ( a, b ) {
			return a.text != b.text ? a.text < b.text ? -1 : 1 : 0;
		}

	};

	return {

		isPhotoLibrary : function () { params.photoLibraryPage = true; },
		
		isCaseStudies : function () {params.caseStudiesPage = true;},

		hasCategories : function () { params.categories = true; },

		//-- used in photolibrary --//
		openProductFromDropDown : function (elem, baseUrl) {
			var productSystemName = elem[elem.selectedIndex].value;
			document.location.href = baseUrl + productSystemName;
		},

		sortSelectBox : function (list) {
			
			if (list) {
				var selected_index = list.selectedIndex,
					selected_value = '',
					items = list.options.length,
					default_text = '';
				
				// create array and make copies of options in list
				var tmpArray = new Array(items-1);
				for ( i=0,j=0; i<items; i++) {
					if (i == selected_index) {
						selected_value = list.options[i].value;
					};
					if(list.options[i].value != "all_products" && list.options[i].value != ""){
					tmpArray[j] = new Option(list.options[i].text, list.options[i].value);
					j++;
					} else{
						default_text = list.options[i].text;
					}
				}
				// sort options using given function
				tmpArray.sort(priv.compareOptionText);
				// make copies of sorted options back to list
				for ( i=0,j=0; i<items; i++ ) {
					if(i == 0){
						list.options[0] = new Option(default_text,"");
					} else {
					list.options[i] = new Option(tmpArray[j].text,tmpArray[j].value);
					if (tmpArray[j].value == selected_value) {
						list.options[i].selected = 'selected';
					};
					j++;
					}
				};
			};
		},

		initiate : function () {
			priv.sortClassifications();
			priv.getClassificationFromUrl();
			priv.toggleNavOptions();
			if (params.photoLibraryPage) {
				productClassification.sortSelectBox(document.getElementById('product_select_list'));
				priv.showAllPhLibItems();
			};
			if (params.caseStudiesPage){
				priv.showAllCsItems();
			}
		}
	};

}();




