if ( !window.HSN ) { window.HSN = {}; }

/*
 * position method
 */
HSN.Mobile = function() {}
HSN.Mobile.prototype.position = function(left, top) {
	var oldTop = this.self['Canvas.Top'];
	var oldLeft = this.self['Canvas.Left'];
	this.self['Canvas.Top'] = top || oldTop;
	this.self['Canvas.Left'] = left || oldLeft;
}

/*
 * set up the promo container and have mini promo buttons inside them
 */
HSN.PromoContainer = function() {
	this.downloader = new Silverlight.Downloader({
		resource : "cmn/xaml/promo.xaml",
		plugin: HSN.plugin,
		onComplete : this.xamlLoaded.bind(this)
	});
}

HSN.PromoContainer.prototype.xamlLoaded = function(downloader, args) {
	var xamlText = downloader.responseText; 
	this.downloader = null;
	
	var xCount = 0;
	var yCount = 0;
	var x = 0;
	var y = 0;
	
	for (var pindex in HSN.anywherePlaylist) {
		// skip over the first (cross channel) and second items (live feed)
		if (pindex > 1) {
			xOffset = xCount * 93;
			yOffset = yCount * 93 + 40;
			HSN.make('PromoButton', { 
				name: 'promo_'+ xCount + '' + yCount, 
				x: xOffset, 
				y: yOffset,
				fragment: HSN.plugin.content.createFromXaml(xamlText, true),
				data: HSN.anywherePlaylist[pindex]
			});
			xCount++;
			if (xCount == 3) { 
				xCount = 0; 
				yCount++; 
			}
		}
	}	
	
	HSN.make('ImageLogo', {name: 'logoImage'}); // empty holder
	HSN.make('Tip', {name: 'promoTip'}); // empty tip
}

/* only one tip is made and moved around 
 * tip listens for broadcasts from the buttons as they are moused over
 */

HSN.Tip = function(params) {
	this.host = params.parent || HSN.root;
	this.name = params.name;
	
	this.downloader = new Silverlight.Downloader({
		resource : "cmn/xaml/tip.xaml",
		plugin: HSN.plugin,
		onComplete : this.xamlLoaded.bind(this)
	});
}

HSN.extend(HSN.Tip, HSN.Mobile);

HSN.Tip.prototype.xamlLoaded = function(downloader, args) {
	var xamlText = downloader.responseText; 
	this.downloader = null;
	
	this.fragment = HSN.plugin.content.createFromXaml(xamlText, true);
	this.host.children.add(this.fragment);
	
	this.self = this.fragment.findName('tip');
	this.textHolder =  this.fragment.findName('tipText');
	this.textShadow =  this.fragment.findName('tipShadow');
	
	this.setText("hi there");
	this.position(50, 50);
}

HSN.Tip.prototype.setText = function(text) {
	this.textHolder.text = text.toUpperCase();
	this.textShadow.width = this.textHolder.actualWidth + 15;
}

HSN.Tip.prototype.showTip = function(text, e) {
	this.setText(text);
	var point = e.getPosition(null);
	
	if (point.y > 290) { point.y = 290; }
	this.position(point.x, point.y);
	
	this.self.Visibility = "Visible";
}

HSN.Tip.prototype.hideTip = function() {
	this.self.Visibility = "Collapsed";
}

/* each individual promo button 
 * has an image and link according to promo data
 */

HSN.PromoButton = function(params) {
	this.host = params.parent || HSN.root;
	this.fragment = params.fragment;
	this.data = params.data; 
	this.x = params.x; 
	this.y = params.y;

	this.host.children.add(this.fragment);
	this.self = this.fragment.findName('promo');
	this.graphic = this.fragment.findName('promoImage');
	this.grow = this.fragment.findName('grow');
	this.shrink = this.fragment.findName('shrink');
	
	this.graphic.Source = this.data.thumbnail;
	
	this.graphic.addEventListener('MouseEnter', this.onOver.bind(this));
	this.graphic.addEventListener('MouseLeave', this.onOut.bind(this));
	this.graphic.addEventListener('MouseLeftButtonUp', this.onPress.bind(this));
	
	this.position(this.x, this.y);
}

HSN.extend(HSN.PromoButton, HSN.Mobile);

HSN.PromoButton.prototype.onOver = function(sender, eArgs) {
	try { this.grow.begin(); } catch(e){}
	
	if (HSN.tracker.Tip.promoTip) {
		HSN.tracker.Tip.promoTip.showTip(this.data.title.toUpperCase(), eArgs);
	}
}

HSN.PromoButton.prototype.onOut = function(sender, eArgs) {
	try { this.shrink.begin(); } catch(e){}
	
	if (HSN.tracker.Tip.promoTip) {
		HSN.tracker.Tip.promoTip.hideTip();
	}
}

HSN.PromoButton.prototype.onPress = function() {
	if (this.data.type == "video") {
		HSN.tracker.DataSwap.dataSwapper.changeVideo(this.data);
		HSN.tracker.DataSwap.dataSwapper.showDiscoverData(this.data);
		HSN.tracker.ImageLogo.logoImage.hide();
		HSN.miniPlayer.setSource(this.data.source, this.data.live);
		HSN.miniPlayer.show();
	}
	if (this.data.type == "image") {
    	HSN.tracker.DataSwap.dataSwapper.showDiscoverData(this.data);
		HSN.tracker.DataSwap.dataSwapper.hideVideo();
		HSN.miniPlayer.hide();
		HSN.tracker.ImageLogo.logoImage.show(this.data.source);
	}

	if ((this.data.title == 'HSN On Demand') || (this.data.title == 'Shop by Remote')) {
    	HSN.Locations(this.data);
	}
}

/* A image object */

HSN.ImageLogo = function(params) {
	this.host = params.parent || HSN.root;
	this.name = params.name;
	
	this.fragment = HSN.plugin.content.createFromXaml('<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="ImageLogo"><Image x:Name="holder" Width="358" Height="361" Stretch="Fill" Source ="" Visibility="Collapsed"/></Canvas>', true);
	this.host.children.add(this.fragment);
	
	this.self = this.fragment.findName('ImageLogo');
	this.holder = this.fragment.findName('holder');
	
	this.position(290, 5);
}
HSN.extend(HSN.ImageLogo, HSN.Mobile);

HSN.ImageLogo.prototype.show = function(url) {
	if (url) {
		this.holder.source = url;
		this.holder.Visibility = "Visible";
	}
}

HSN.ImageLogo.prototype.hide = function() {
	this.holder.Visibility = "Collapsed";
}

/*
 * make a live feed button
 */
HSN.LiveFeedButton = function(params) {
	this.host = params.parent || HSN.root;
	this.name = params.name;
	this.disabled = false;
	
	this.fragment = HSN.plugin.content.createFromXaml('<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="LiveFeed"><Image Width="266" Height="25" Stretch="Fill" Source ="cmn/img/btn_livefeed_on.png" Cursor="Hand"/><Image x:Name="over" Width="266" Height="25" Stretch="Fill" Source ="cmn/img/btn_livefeed_over.png" Cursor="Hand" Visibility="Collapsed"/><Rectangle x:Name="disabled" Width="266" Height="25" Fill="White" Opacity="0.6" Visibility="Collapsed"/></Canvas>', true);
	this.host.children.add(this.fragment);
	
	this.self = this.fragment.findName('LiveFeed');
	this.overState = this.fragment.findName('over');
	this.disableState = this.fragment.findName('disabled');
	
	this.self.addEventListener('MouseEnter', this.onOver.bind(this));
	this.self.addEventListener('MouseLeave', this.onOut.bind(this));
	this.self.addEventListener('MouseLeftButtonUp', this.onPress.bind(this));
	
	this.position(5, 325);
}

HSN.extend(HSN.LiveFeedButton, HSN.Mobile);

HSN.LiveFeedButton.prototype.onOver = function() {
	this.overState.Visibility = "Visible";
}

HSN.LiveFeedButton.prototype.onOut = function() {
	if (!this.disabled) {
		this.overState.Visibility = "Collapsed";
	}
}

HSN.LiveFeedButton.prototype.onPress = function() {
    window.location = 'index.html';
}

HSN.LiveFeedButton.prototype.onPageLoad = function() {
	this.overState.Visibility = "Visible";
	this.disableState.visibility = "Visible";
	this.disabled = true;
}

/* 
 * change html data according to current playlist 
 */
HSN.DataSwap = function() {
	// grab all the dom elements here so we dont have to look for them later
	this.videoSection = document.getElementById('videos');
	this.nowPlaying = document.getElementById('NowPlaying');
	this.title = document.getElementById('title');
	this.description = document.getElementById('description');
}

HSN.DataSwap.prototype.changeVideo = function(data) {
	this.videoSection.style.visibility = "visible";
	this.nowPlaying.innerHTML = data.title.toUpperCase();
}

HSN.DataSwap.prototype.hideVideo = function(data) {
	this.videoSection.style.visibility = "hidden";
}

HSN.DataSwap.prototype.showDiscoverData = function(data) {
	this.title.innerHTML = "Discover " + data.title;
	this.description.innerHTML = "<p>" + data.description;
}

/* 
 * location menu
 * used in hsn on demand and shop by remote sections
 */
HSN.Locations = function(section) {
    var section = section;
    var locations = document.getElementById('locations');
    var states = locations.getElementsByTagName('span');
    var videos = locations.getElementsByTagName('a');

	// show/hide locations
	for (var i=0; i<states.length; i++) {
	    YAHOO.util.Event.addListener(states[i], 'click', function (e) {
            var state = this.parentNode;

            if (state.className == 'expanded') {
                state.className = '';
            } else {
                for (var j=0; j<states.length; j++) {
                    states[j].parentNode.className = '';
                }
                state.className = 'expanded';
            }

        	YAHOO.util.Event.preventDefault(e);
        });
	}

	// update video
	for (var i=0; i<videos.length; i++) {
	    YAHOO.util.Event.addListener(videos[i], 'click', function (e) {
            var video = this.getAttribute('href');

        	HSN.miniPlayer.setSource(video);
			HSN.miniPlayer.show();
        	YAHOO.util.Event.preventDefault(e);
        });
	}
}

/*
 * the plugin has loaded, create components inside the plugin
 * ready dom elements
 */
HSN.init = function(sender, context, root) {
	HSN.plugin = sender;					
	HSN.root = root;	
	
	var pluginHost = document.getElementById('pluginBox'); 
	pluginHost.style.background = '';

	HSN.setUpFeedback();
	//HSN.remote = new HSN.Remote(80);
	HSN.miniPlayer = new HSN.Player({ type: 'mini', source: HSN.anywherePlaylist[0].source } );
	
	HSN.make('PromoContainer', { name: 'promoStuff'});
	//HSN.make('LiveFeedButton', { name: 'livefeedButton'});
	HSN.make('DataSwap', { name: 'dataSwapper'});

	HSN.tracker.DataSwap.dataSwapper.showDiscoverData(HSN.anywherePlaylist[0]);
}

/*
 * set up plugin
 */
HSN.setUpSilverlight = function() {
	var pluginSpecs = { 
		height : '372px', width: '654px', 
		host : 'pluginBox', 
		xaml : 'cmn/xaml/anywhere.xaml', 
		id : 'myPlugin', 
		color : '#00000000',
		postInstall : true,
		events : {
	        onLoad: HSN.init
		}
	};
	Silverlight.createInstallExperience(pluginSpecs); // post install optional
}

HSN.DomLoaded.load(HSN.setUpSilverlight);
