/**
 * $Id: [project name].js [yyyy-mm-dd] $
 *
 * @author [Developer Name]
 * @copyright Copyright (c) [yyyy], Trapeze, All rights reserved.
 *
 * Dependencies:
 *      jQuery 1.3.1        (http://www.jquery.com),
 */

$.namespace("trapeze.thinsations");

trapeze.thinsations = $.Class.extend({
									 
	defaultSearchInputValue : null,
	
    replaceFonts : function() {
        
        Cufon.replace('h1');
        Cufon.replace('#comments h2');
        Cufon.replace('.font-replacement');
    },
	
	autoEraseSearchForm : function() {
		
		this.defaultSearchInputValue = $("#GlobalSearchInput").attr("value");
		
		var that = this;
		$("#GlobalSearchInput").focus(function() {
			
			if (this.value == that.defaultSearchInputValue) {
				this.value = "";
			}
		});
		
		$("#GlobalSearchInput").blur(function() {
			
			if (this.value == "") {
				this.value = that.defaultSearchInputValue;
			}
		});
	},
	
	initProducts : function() {
		
		$("#ProductList a").each(function() {
			
			var tabURLLocation = window.location.href.indexOf("#");
			if (tabURLLocation == -1) {
				tabURLLocation = window.location.href.length;
			}
			this.href = this.href + window.location.href.substring(tabURLLocation);
		});
		
		$("#SubNavigation-nav a").click(function() {

			var tab = $(this).attr("href");
			tab = tab.substring(tab.indexOf("#"));
			$("#ProductList a").each(function() {
				var tabURLLocation = this.href.indexOf("#");
				if (tabURLLocation == -1) {
					tabURLLocation = this.href.length;
				}
				this.href = this.href.substring(0, tabURLLocation) + tab;
			});
		});
	},
	
	initTips : function() {
		
		var tipNumber = parseInt(window.location.href.substring(window.location.href.indexOf("#")+1));
		
		if (!tipNumber) {
			tipNumber = 1;
			window.location = window.location.href + "#" + tipNumber;
		}
		
		// Listen for changes to the URL and change the tip number accordingly
		setInterval(function() {

			// Keep track of the old and new tip numbers
			var oldTipNumber = tipNumber;
			var newTipNumber = parseInt(window.location.href.substring(window.location.href.indexOf("#")+1));
			
			// If newTipNumber is a number and is different than the current tip, change the tip in the Flash
			if (newTipNumber && newTipNumber != tipNumber) {
				
				// We need to update the URL in case invalid characters were originally present
				tipNumber = newTipNumber;
				window.location = window.location.href.substring(0, window.location.href.indexOf("#")+1) + tipNumber;

				// Call a Flash function that changes the card shown on the screen
				// If it returns false, the tip # was out of bounds and not changed, so revert the URL back
				if (trapeze.thinsations.prototype.tipsMovie && !trapeze.thinsations.prototype.tipsMovie.gotoCard(tipNumber)) {
					tipNumber = oldTipNumber;
					window.location = window.location.href.substring(0, window.location.href.indexOf("#")+1) + tipNumber;
				}
			}
			
			// Update the URL one more time in case Flash didn't update (due to tip # out-of-bounds)
			//window.location = window.location.href.substring(0, window.location.href.indexOf("#")+1) + tipNumber;

		}, 200);
	},
	
	printTips : function() {
		
		print();
	},
	
	// This function is called by Flash to change the tip # in the URL.
	// This, in turn will call a Flash function that changes the card.
	gotoTip : function(tipNumber) {
		
		var url = window.location.href.substring(0, window.location.href.indexOf("#")+1) + tipNumber;
		window.location = url;
	},
	
	getXHTML : function(selector) {
		
		var html = '<ul>' + innerXHTML(document.getElementById("TipList")) + '</ul>';
		html = html.replace(/"/g, "'"); // IE doesn't like double quotes, so replace them with single ones
		return html;
	},
    
    init : function() {
        var page = $('body').attr('class');
        
        //page = page.match(/(\w+-page)/);
        page = (page.indexOf(' ') > 0) ? page.slice(0,page.indexOf(' ')) : page;
        switch(page) {
			case 'product-detail':
				this.initProducts();
				break;
			case 'tips':
				this.initTips();
				replaceTips();
				break;
			case 'tips-print':
				this.printTips();
				break;
            default:
                //Code to be executed on any page, but the pages listed above.
        }
        
        $('a.new-window').click(function() {
            window.open(this.href);
            return false;
        });
        
        this.replaceFonts();
		this.autoEraseSearchForm();
    }
});

$(function() {
    new trapeze.thinsations();
});
