﻿var GP = {	
	initialize: function () {
		this.GrandThoughts.initialize();
		this.Toys.initialize();
		this.PopBrands.updateWT();
	}

};

GP.Toys = {
	config: {
		constant: {
			controller: '/fp.aspx',
			state: '2515'
		}
	},
	initialize: function () {
		this.cacheSelectors();		
		this.ProductSearch.initialize();
		this.UI.initialize();	
		this.attachEvents();
	},
	attachEvents: function () {
		var self = this;

		//this.Category.$items.click(function (e) { self.filter(self.Category, this) });
		this.ProductSearch.$sort_items.click(function (e) { self.ProductSearch.setSortOrder(this) });		
	},
	cacheSelectors: function () {
		this.ProductSearch.$sort = $(this.ProductSearch.config.selector.sort.container);
		this.ProductSearch.$sort_items = $(this.ProductSearch.config.selector.sort.items, this.ProductSearch.$sort);
		this.ProductSearch.$category = $(this.ProductSearch.config.selector.category.container);
		//this.ProductSearch.$category_items = $(this.ProductSearch.config.selector.category.items, this.ProductSearch.$category);
		this.ProductSearch.$age = $(this.ProductSearch.config.selector.age.container);	
		this.ProductSearch.$event = $(this.ProductSearch.config.selector.e.container);	
	}
};
GP.Toys.UI = {
	initialize: function () {
		this.attachEvents();
	},
	attachEvents: function () {
		GP.Toys.ProductSearch.$sort.addHoverState();
		GP.Toys.ProductSearch.$category.addHoverState();
	}
};
GP.Toys.ProductSearch = {
	$sort: null,
	$sort_items: null,
	$category: null,
	$category_items: null,
	$age: null,
	$event: null,

	sort: null,
	page_size: null,
	age: null,
	category: null,
	e: null,
	
	config: {
		constant: {
			event: 'thumbnail'
		},
		
		initial: {
			sort: {
				value: 'price',
				direction: 'desc'
			},
			
			page_size: 10,
			
			age: {
				min: 0,
				max: 72
			},
			
			category: {
				catparcode: 'ToyFinder',
				catcode: ''
			}
		},
		
		attribute: {
			sort: {
				value: 'data-sort',
				direction: 'data-sort-direction'
			},
			age: {
				min: 'min-age',
				max: 'max-age'
			},
			category: {
				catparcode: 'cat-par-code',
				selectedcatcode: 'sel-cat-code',
				catcode: 'data-cat-code'
			},
			e: 'event'
		},
		
		selector: {
			sort: {
				container: '#sort',
				items: 'li'
			},
			category: {
				container: '#category',
				items: 'li'
			},
			age: {
				container: '#sort'
			},
			e: {
				container: '#sort'
			}
		}
	},	
	
	initialize: function () {
		this.sort = this.config.initial.sort;
		this.page_size = this.config.initial.page_size;
		this.age = this.config.initial.age;
		this.age.min = this.$age.attr(this.config.attribute.age.min);
		this.age.max = this.$age.attr(this.config.attribute.age.max);
		this.category = this.config.initial.category;
		this.category.catparcode = this.$category.attr(this.config.attribute.category.catparcode);
		this.category.catcode = this.$category.attr(this.config.attribute.category.selectedcatcode);
		this.e = this.config.constant.event;
	},
	
	setPageSize: function (obj) {
	
	},
	
	setSortOrder: function (obj) {
		var $obj = $(obj);
	
		this.sort.value = $obj.attr(this.config.attribute.sort.value);
		this.sort.direction = $obj.attr(this.config.attribute.sort.direction);
		this.e = GP.Toys.ProductSearch.$event.attr(this.config.attribute.e);
		
		var query = GP.Toys.config.constant.controller +
			'?st=' + GP.Toys.config.constant.state +
			'&e=' + this.e +
			'&catparcode=' + this.category.catparcode +
			'&cminage=' + this.age.min +
			'&cmaxage=' + this.age.max +
			'&pminage=' + this.age.min +
			'&pmaxage=' + this.age.max +
			'&psort=' + this.sort.value +
			'&psortdirection=' + this.sort.direction;
		
		if(this.category.catcode!=null && this.category.catcode.length>0)
			query += '&pcat=' + this.category.catcode;
		
		var $obj_link = $('a', $obj);
		$obj_link.attr('href',query);
		$obj_link.click;
	}
};

GP.GrandThoughts = {
	error_firstname: null,
	error_title: null,
	error_comments: null,

	config: {
		error_firstname_container_id: 'err-firstname',
		error_title_container_id: 'err-title',
		error_comments_container_id: 'err-comments'
	},

	setObjects: function () {
		this.error_firstname = $('#' + this.config.error_firstname_container_id);
		this.error_title = $('#' + this.config.error_title_container_id);
		this.error_comments = $('#' + this.config.error_comments_container_id);
	},
	
	hideObjects: function () {
		this.error_comments.hide();
		this.error_firstname.hide();
		this.error_title.hide();
	},

	initialize: function () {
		this.setObjects();
		this.hideObjects();
	},
	
	validateForm: function (default_name_text, default_title_text, options) {
		var defaults = {
			error_tooltip: true
		};
		
		var opts = $.extend({}, defaults, options);
		
		$('#grandthoughtsForm input[name=\'firstname\']').focus(function () {
			var $this = $(this);
			if ($.trim($this.val()) == default_name_text) {
				$this.val('');
			}
		});
		
		$('#grandthoughtsForm input[name=\'calledByGC\']').focus(function () {
			var $this = $(this);
			if ($.trim($this.val()) == default_title_text) {
				$this.val('');
			}
		});
		
		$('#grandthoughtsForm').submit(function () {
			var $form = $(this);
			var $firstname = $('input[name=\'firstname\']', $form);
			var $title = $('input[name=\'calledByGC\']', $form);
			var $comments = $('textarea[name=\'grandThoughts\']', $form);
			
			var name_ready = false;
			var title_ready = false;
			var comments_ready = false;
			
			if ($.trim($firstname.val()) != default_name_text && $firstname.val() != '') {
				name_ready = true;
			}
			if ($.trim($title.val()) != default_title_text && $title.val() != '') {
				title_ready = true;
			}
			if ($comments.val() != '') {
				comments_ready = true;
			}
			
			GP.GrandThoughts.hideObjects();
			
			if (name_ready && title_ready && comments_ready) {
				return true;
			} else {
				if(!name_ready) {
					GP.GrandThoughts.error_firstname.show();
				}
				if(!title_ready) {
					GP.GrandThoughts.error_title.show();
				}
				if(!comments_ready) {
					GP.GrandThoughts.error_comments.show();
				}
				return false;
			}
		});
	}
};

GP.PopBrands = {	
	updateWT: function() {
		$container = $('#brand-tiles');		
		$('li a', $container).each(function () {
			// update tracking
			var wt8 = $(this).attr('wt8track');
			var wt7 = $(this).attr('track');
			if(wt8 != 'undefined') { 
				var oriOptions = wt8.split('|');
				var newOptions = oriOptions;
				newOptions[0] = (newOptions[0].indexOf('-') > 0) ? 'Grandparents Popular Brands ' + newOptions[0].substring(newOptions[0].indexOf('-')) : 'Grandparents Popular Brands - ' + newOptions[0];
				newOptions[1] = 'NONE';
				newOptions[2] = 'Grandparents';
				newOptions[3] = 'BUTTON';
				newOptions[4] = 'Click';
				$(this).attr('wt8track', newOptions.join("|"));
			}
			if(wt7 != 'undefined') { 
				var newWT7 = (wt7.indexOf('-') > 0) ? 'Grandparents Popular Brands ' + wt7.substring(newOptions[0].indexOf('-')) : 'Grandparents Popular Brands - ' + wt7;
				$(this).attr('track', newWT7);
			}
			// add popup
			$(this).attr('rel', 'popup|1000|700|scrollbars|resizable|location|toolbar');
			// update image path and image extention
			$img = $('img', $(this));
			if($img != null) {
				var src = $img.attr('src');
				src = '/content/v6/us/grandparents/img/brand_tiles/' + ((src.lastIndexOf('/')>0) ? src.substring(src.lastIndexOf('/')+1) : src )
				src = (src.lastIndexOf('.')>0) ? src.substring(0,src.lastIndexOf('.')+1)+'jpg' : src;
				$img.attr('src', src);
			}
		});
		if(SiteHelper!='undefined')
			SiteHelper.detectPopups(); 
	}
		
};
	
$(document).ready(function () {
	GP.initialize();
});

