/** 
 * Create the autocomplete search block
 */
function linkSearchAutocomplete() {
    new Ajax.Autocompleter('smart_search', 'smart_search_results', '/customer/smart_search.php', {
        afterUpdateElement: loadProductDetails,
        onShow: function(element, update) {
                if(!update.style.position || update.style.position=='absolute') {
                    update.style.position = 'absolute';
                    Position.clone(element, update, {
                        setHeight: false,
                        setWidth: false,
                        offsetTop: element.offsetHeight
                    });
                }
                Effect.Appear(update,{duration:0.15});
            }
    });
}

function loadProductDetails(text, li) {
    window.location = li.title;
}

// Autocomplete turned off for now
Event.observe(window, 'load', linkSearchAutocomplete);

// Show the window dialog
showSelectCountry = function() {
	Dialog.info({url: '/select_country.php'},
		{
		id: 'select_country',
		className: 'alphacube',
		width: 560,
		height: 420,
		zIndex: 20000
		}
	);
	return false;
}

// Check for cookie (set in select_country.tpl).  This cookie
// is independent of x-cart so the js doesn't step on the toes
// of the underlying functionality and interfere with non-js
// browsers
isCountrySelected = function() {
	if (!Get_Cookie('language')) {
		showSelectCountry();
	}
}

// Submit the select language form from a popup
setCountry = function(country, forward_url) {
	Set_Cookie('language', country, 365, '/', 'powermusic.com');
	if (window.location.hostname == 'localhost') Set_Cookie('language', country, 365);
	if (forward_url) {
		window.location = forward_url;
	} else {
		window.location.reload();
	}
	return false;
}

Event.observe(window, 'load', isCountrySelected);
Event.observe(window, 'load', loadImageSwaps);

// Media player functions
playSong = function(trackid) {
	new Ajax.Request ('/customer/get_media.php', {
		'parameters': 'trackid=' + trackid,
		'method': 'post',
		'onSuccess': function(html, json) {
			$('player').sendEvent('LOAD', json.track);
			$('player').sendEvent('PLAY');
		}
	});
}

playAlbum = function(productid) {
	new Ajax.Request ('/customer/get_media.php', {
		'parameters': 'productid=' + productid,
		'method': 'post',
		'onSuccess': function(html, json) {
			$('player').sendEvent('LOAD', json.tracklist);
			$('player').sendEvent('PLAY');
		}
	});
}

linkSamples = function () {
	$$('.sample').each(function(a) {
		new sample(a);
	});
}

var sample = Class.create();
sample.prototype = {
	watch: null,
	initialize: function(a) {
		this.watch = a;
		this.url = a.href;
		this.watch.onclick = this.onclick.bindAsEventListener(this);
	},
	onclick: function() {
		var track = {
			author: 'Author',
			description: '',
			file: this.url,
			link: '',
			image: '',
			start: 0,
			title: '',
			type: 'sound'
		};

		$('player').sendEvent('LOAD', track);
		$('player').sendEvent('PLAY');

		return false;
	}
}

Event.observe(window, 'load', linkSamples);

// Mouseover image swapping class
// USAGE: new imageSwapper(img, img.src, swapurl);
var imageSwapper = Class.create();
imageSwapper.prototype = {
	initialize: function(img, normal, hover) {
		this.watch = img;
		this.normal = normal;
		this.hover = hover;
		this.enable();
		Event.observe(this.watch, 'mouseover', this.onmouseover.bind(this));
		Event.observe(this.watch, 'mouseout', this.onmouseout.bind(this));
		this.watch.disable = this.disable.bind(this);
		this.watch.enable = this.enable.bind(this);
	},
	onmouseover: function() {
		if (!this.disabled) this.watch.src = this.hover;
	},
	onmouseout: function() {
		if (!this.disabled) this.watch.src = this.normal;
	},
	disable: function() {
		this.disabled = true;
	},
	enable: function() {
		this.disabled = false;
	}
}

function loadImageSwaps() {
	if ($('btn_viewcart')) new imageSwapper($('btn_viewcart'), $('btn_viewcart').src, '/skin1/images/btn_viewcart_lg_active.gif');
	if ($('btn_addtocart')) new imageSwapper($('btn_addtocart'), $('btn_addtocart').src, '/skin1/images/btn_addtocart_active.gif');
}

// Helper links / lightboxes
showDownload1 = function() {
	Dialog.alert({url: '/downloads1.html'},
		{
		id: 'select_country',
		className: 'alphacube',
		width: 680,
		height: 688,
		zIndex: 20004
		}
	);
	return false;
}

showDownload2 = function() {
	Dialog.alert({url: '/downloads2.html'},
		{
		id: 'downloads2',
		className: 'alphacube',
		width: 680,
		height: 640,
		zIndex: 20002
		}
	);
	return false;
}

showArtists = function() {
	Dialog.alert({url: '/original_artists.html'},
		{
		id: 'artists',
		className: 'alphacube',
		width: 360,
		height: 175,
		zIndex: 20003
		}
	);
	return false;
}

showGatorade = function() {
	Dialog.alert({url: '/gatorade_sponsorship.html'},
		{
		id: 'artists',
		className: 'alphacube',
		width: 600,
		height: 350,
		zIndex: 20005
		}
	);
	return false;
}