window.addEvent('domready',function() {
	new sSearch();
	var pUri = parseUri(document.location.href);
	var reg = new RegExp('^/product/.*');
	if(reg.test(pUri.path) === true || pUri.path == '/theater.html' || pUri.path == '/unisen.html' || pUri.path == '/laser.html') {
		var bg = $('divBG').effects({duration: 5000});
		bg.start({'opacity': [0,.45],'-moz-opacity': [0,.45]}).chain(function() {this.start({'opacity': .35,'-moz-opacity': .35});});
		var tvTop = $('imgTVTop').effects({duration: 1500});
		tvTop.start.delay(1500,tvTop,{'margin-top':'0px'});
		var tvBot = $('imgTVBot').effects({duration: 1500});
		tvBot.start.delay(1500,tvBot,{'margin-top':'0px','opacity': [0,.7],'-moz-opacity': [0,.7]});
		var cC = $('contentCont').effects({duration:1000});
		cC.start.delay(1750,cC,{'opacity': [0,1],'-moz-opacity': [0,1]});
		$$('ul.tabs li').each(function(el,i) {$(el).setStyle('cursor','pointer');$(el).addEvent('click',function() {showCont((i - 1));});});
		var lNav = $('leftNav').effects({duration:1000});
		lNav.start.delay(3750,lNav,{'opacity': [0,1],'-moz-opacity': [0,1]});
		var ftrNav = $('footer').effects({duration:1000});
		ftrNav.start.delay(1500,ftrNav,{'opacity': [0,1],'-moz-opacity': [0,1]});
		var mainNavFade = $('mainFamNav').effects({duration:1000});
		mainNavFade.start.delay(1500,mainNavFade,{'opacity': [0,1],'-moz-opacity': [0,1]});
		var tM = $('topMenu').effects({duration:1000});
		tM.start.delay(3750,tM,{'opacity': [0,1],'-moz-opacity': [0,1]});
	} else if(pUri.path == '/' || pUri.path == '/index.html') {
		var lNav = $('leftNav').effects({duration:1000});
		lNav.start.delay(1500,lNav,{'opacity': [0,1],'-moz-opacity': [0,1]});
		var mainNavFade = $('mainFamNav').effects({duration:1000});
		mainNavFade.start.delay(1500,mainNavFade,{'opacity': [0,1],'-moz-opacity': [0,1]});
		var tM = $('topMenu').effects({duration:1000});
		var ftrNav = $('footer').effects({duration:1000});
		ftrNav.start.delay(1500,ftrNav,{'opacity': [0,1],'-moz-opacity': [0,1]});
		tM.start.delay(1500,tM,{'opacity': [0,1],'-moz-opacity': [0,1]});
	} else {
		if ($chk($('leftNav'))){
			var lNav = $('leftNav').effects({duration:1000});
			lNav.start.delay(200,lNav,{'opacity': [0,1],'-moz-opacity': [0,1]});
			var ftrNav = $('footer').effects({duration:1000});
			ftrNav.start.delay(200,ftrNav,{'opacity': [0,1],'-moz-opacity': [0,1]});
			var mainNavFade = $('mainFamNav').effects({duration:1000});
			mainNavFade.start.delay(200,mainNavFade,{'opacity': [0,1],'-moz-opacity': [0,1]});
			var tM = $('topMenu').effects({duration:1000});
			tM.start.delay(200,tM,{'opacity': [0,1],'-moz-opacity': [0,1]});
		}
	}
});

function correctPNG() {
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (document.body.filters)) {
		for(var i=0; i<document.images.length; i++) {
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}    
}

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

var pCar = new Class({
	options: {
		initialMarg: 0, //sets the initial position of the carousel
		initialOpac: 1, //sets opacity of item elements in carousel
		onHover: 1, //if initialOpac < 1.0, set opacity to what level?
		idPrev: 'carousel_prev', //id of previous control image
		idNext: 'carousel_next', //id of next control image
		aImgPrev: null,
		aImgNext: null,
		sImgPrev: null,
		sImgNext: null,
		carClass: 'carousel_cont', //class name of carousel container
		totalWidth: 0, //total width of carousel.  will be set automagically
		maxLeft: 0, //maximum (negative) margin-left that will be set
		
		item: {
			//either option can be set below
			//finds the carousel items by class name or html tag
			//USE ONLY ONE.  If both are set, ignores to iTagName!
			iTagName: '', //html tag of carousel items
			iClassName: 'carousel_item', //class name of carousel items
			iWidth: 0, //width (in pixels) of carousel items.  make sure to take padding, margin and border into consideration
			iShown: 0  //number of items shown in carousel
		},
		
		animation: {
			type: "scroll", // fScrollf, scroll
			amount: 1, // if type = scroll, set the amount to scroll
			mTime: 1, //amount of time (in seconds) to animate the scroll
			rotate: {
				aScroll: false, // automatic scrolling on init: true | false
				aScrollInt: 5, // scrolling interval (in seconds) in aScroll == true
				aScrollStop: true // if aScroll == true, set the onmouseover behavior to stop the scroll: true | false
			},
			transition: Fx.Transitions.Back.easeOut
		}
	},
		
	initialize: function(carId,options) {
		if(! $(carId)) return false;
		this.carousel = $(carId);
		this.setOptions(options);
		this.status = false;
		this.options.sImgPrev = $(this.options.idPrev).getAttribute('src');
		this.options.sImgNext = $(this.options.idNext).getAttribute('src');
		
		this.subItems = (this.options.item.iTagName != '') ?  this.carousel.getElementsByTagName(this.options.item.iTagName) : this.carousel.getElementsByClassName(this.options.item.iClassName);
		this.numItems = this.subItems.length;
		this.options.totalWidth = (this.numItems * this.options.item.iWidth);
		this.options.maxLeft = ((this.numItems - this.options.item.iShown) * this.options.item.iWidth);
		this.carCont = this.carousel.getElementsByClassName(this.options.carClass)[0];
		this.fx = new Fx.Style(this.carCont,'margin-left',{
			duration: (this.options.animation.mTime * 1700),
			transition: this.options.animation.transition,
			wait: false
		});
		this.fx.start(this.options.initialMarg+'px');
		this.moved = 0;
		
		$(this.options.idPrev).addEvents({
			"click": function() {
				this.prev();
			}.bind(this),
			"mouseover": function() {
				$(this.options.idPrev).setAttribute('src',this.options.aImgPrev);
			}.bind(this),
			"mouseout": function() {
				$(this.options.idPrev).setAttribute('src',this.options.sImgPrev);
			}.bind(this)
		});
		
		$(this.options.idPrev).setStyle('opacity',0);
		
		$(this.options.idNext).addEvents({
			"click": function() {
				this.next();
			}.bind(this),
			"mouseover": function() {
				$(this.options.idNext).setAttribute('src',this.options.aImgNext);
			}.bind(this),
			"mouseout": function() {
				$(this.options.idNext).setAttribute('src',this.options.sImgNext);
			}.bind(this)
		});
		
		return true;
	},
		
	next: function() {
		var carMarg = parseInt(this.carCont.getStyle('margin-left'));
		if(carMarg == 0) {
			var nextMarg = (this.options.item.iWidth * this.options.animation.amount);
			$(this.options.idPrev).setStyle('cursor','pointer');
			$(this.options.idPrev).setStyle('opacity',1);
			if(nextMarg >= this.options.maxLeft) {
				nextMarg = this.options.maxLeft;
				this.fx.start('-'+nextMarg+'px');
				$(this.options.idNext).setStyle('cursor','default');
				$(this.options.idNext).setStyle('opacity',0);
			} else {
				this.fx.start('-'+nextMarg+'px');
			}
		} else {
			var nextMarg = ((this.options.item.iWidth * this.options.animation.amount) - carMarg);
			if(nextMarg >= this.options.maxLeft) {
				nextMarg = this.options.maxLeft;
				$(this.options.idNext).setStyle('cursor','default');
				$(this.options.idNext).setStyle('opacity',0);
				this.fx.start('-'+nextMarg+'px');
			} else {
				this.fx.start('-'+nextMarg+'px');
			}
		}
	},
		
	prev: function() {
		var carMarg = parseInt(this.carCont.getStyle('margin-left'));
		if(carMarg == -this.options.maxLeft) {
			var nextMarg = ((this.options.item.iWidth * this.options.animation.amount) + carMarg);
			if(nextMarg < 0) {
				$(this.options.idNext).setStyle('cursor','pointer');
				$(this.options.idNext).setStyle('opacity',1);
				this.fx.start(nextMarg+'px');
			} else {
				$(this.options.idPrev).setStyle('cursor','default');
				$(this.options.idPrev).setStyle('opacity',0);
				$(this.options.idNext).setStyle('cursor','pointer');
				$(this.options.idNext).setStyle('opacity',1);
				this.fx.start('0px');
			}
		} else {
			var nextMarg = ((this.options.item.iWidth * this.options.animation.amount) + carMarg);
			if(nextMarg < 0) {
				this.fx.start(nextMarg+'px');
			} else {
				$(this.options.idPrev).setStyle('cursor','default');
				$(this.options.idPrev).setStyle('opacity',0);
				this.fx.start('0px');
			}
		}
	}
});
pCar.implement(new Options);

function showCont(il) {
	$$('#tabCont div.tab').each(function(el,i) {
		if(il == i) {
			$(el).setStyle('display','block');
		} else {
			$(el).setStyle('display','none');
		}
	});
	
	var lis = $$('ul.tabs li');
	if($(lis[0]).hasClass('ht')) {
		var lC = 'sel_ht';
	} else if($(lis[0]).hasClass('fp')) {
		var lC = 'sel_fp';
	} else if($(lis[0]).hasClass('lv')) {
		var lC = 'sel_lv';
	} else if($(lis[0]).hasClass('isp')) {
		var lC = 'sel_isp';
	}
	
	lis.each(function(el,i) {
		if((il + 1) == i) {
			$(el).addClass(lC);
		} else {
			$(el).removeClass(lC);
		}
	});
}
        
var jSel = new Class({
    options: {
        selbox: 'selCont',
        upImg: 'img/locator_sel_up.gif',
        dnImg: 'img/locator_sel_dn.gif',
        values: [],
        duration: 500,
        transition: Fx.Transitions.linear
    },
    initialize: function(carId,options) {
		this.selopts = new Element('div',{
            'id': 'selOpts',
            'styles': {
                'color': '#FFF',
                'font-family': 'Arial,Helvetica,sans-serif',
                'font-size': '10pt',
                'position': 'relative',
                'left': '7px',
                'top': '4px',
                'width': '60px',
				'float': 'left'
            }
        }).injectInside(this.options.selbox);
        this.selarr = new Element('div',{
            'id': 'selArr',
            'styles': {
                'float': 'right',
				'margin-top': '3px',
				'margin-right': '3px',
				'width': '10px'
            }
        }).injectInside(this.options.selbox);
        this.uparr = new Element('img',{
            'id': 'selUp',
            'src': this.options.upImg,
            'styles': {
                'display': 'block',
				'cursor': 'pointer'
            },
			'events': {
				'click': function() {
					this.dn();
				}.bind(this)
			}
        }).injectInside(this.selarr);
		this.dnarr = new Element('img',{
            'id': 'selDn',
            'src': this.options.dnImg,
            'styles': {
                'display': 'block',
                'cursor': 'pointer'
            },
			'events': {
				'click': function() {
					this.up();
				}.bind(this)
			}
        }).injectInside(this.selarr);
        this.input = new Element('input',{
            'type': 'hidden',
            'name': 'selVal',
            'id': 'selVal',
            'value': 0
        }).injectInside(this.options.selbox);
        this.fx = [];
        this.curdiv = 1;
    },
    addopt: function(key,val,status) {
        this.options.values.include(key);
        if(status == true) {
			this.input.value = key;
			var selOptNew = new Element('div',{
				'id': 'o'+key,
				'class': 'selOptions',
				'styles': {
					'height': '20px',
					'width': '65px',
					'position': 'absolute'
				}
			}).setHTML(val).injectInside(this.selopts);
        } else {
			var selOptNew = new Element('div',{
				'id': 'o'+key,
				'class': 'selOptions',
				'styles': {
					'height': '20px',
					'width': '65px',
					'opacity': 0,
					'-moz-opacity': 0,
					'position': 'absolute'
				}
			}).setHTML(val).injectInside(this.selopts);
		}
        var count = -1;
		this.options.values.each(function(el,i) {
			count++;
		});
		this.fx[count] = new Fx.Style('o'+key, 'opacity', {
			duration: this.options.duration,
			transition: this.options.transition,
			wait: false
		});
    },
    up: function() {
		if(this.curdiv > 0) {
			for(var i = 0; i < this.options.values.length; i++) {
				if(i != (this.curdiv - 1)) this.fx[i].start(0);
			}
            this.curdiv--;
			this.fx[this.curdiv].start(1);
            this.input.value = this.options.values[this.curdiv];
		}
    },
    dn: function() {
		if(this.curdiv < (this.options.values.length - 1)) {
			for(var i = 0; i < this.options.values.length; i++) {
				if(i != (this.curdiv + 1)) this.fx[i].start(0);
			}
            this.curdiv++;
			this.fx[this.curdiv].start(1);
            this.input.value = this.options.values[this.curdiv];
		}
    }
});

var rotHome = new Class({
	initialize: function() {
		this.i = 0;
		this.interval;
		this.n;
		this.fx = [];
		this.images = $$('div.mits_hp img');
		this.links = $$('#leftNav a');
		this.links.each(function(el,i) {
			this.fx[i] = new Fx.Styles(this.images[i],{duration: 1500, wait: false});
			el.addEvents({
				'mouseenter': function() {
					this.pause(i);
				}.bind(this),
				'mouseleave': function() {
					this.resume();
				}.bind(this)
			}, this);
		}, this);
		this.start();
	},
	start: function() {
		this.fx[this.i].start.delay(2500,this.fx[this.i],{'opacity': [0,1]});
		this.interval = this.rotate.delay(7500,this);
	},
	pause: function(i) {
		//$clear(this.interval);
	},
	resume: function() {
		//this.interval = this.rotate.delay(6500,this);
	},
	rotate: function() {
		this.n = (this.i == (this.images.length - 1)) ? 0 : (this.i + 1);
		this.show();
		this.interval = this.rotate.delay(6500,this);
	},
	show: function() {
		this.fx[this.i].start({'opacity': 0});
		this.fx[this.n].start.delay(1500,this.fx[this.n],{'opacity': 1});
		this.i = this.n;
	}
});


function locate() {
	var frmError = false;
	var zipMatch = new RegExp('[0-9]{5}');
	if($('locZip').value == '') {
		frmError = true;
	} else if(! zipMatch.test($('locZip').value)) {
		frmError = true;
	}
	
	if(! frmError) {
		$('rlLink').setAttribute('href','retaillocator_mbox.html?radius='+$('selVal').value+'&zip='+$('locZip').value);
		var lBox = new MultiBox('rlLink');
		lBox.open($('rlLink'));
		$('locZip').setStyle('background','url(/img/locator_input.png) bottom right no-repeat');
	} else {
		$('locZip').setStyle('background','url(/img/locator_input_err.gif) bottom right no-repeat');
	}
}

function locate_mbox() {
	var frmError = false;
	var zipMatch = new RegExp('[0-9]{5}');
	if($('locZip').value == '') {
		frmError = true;
	} else if(! zipMatch.test($('locZip').value)) {
		frmError = true;
	}
	
	if(! frmError) {
		document.location.href='retaillocator_mbox.html?radius='+$('selVal').value+'&zip='+$('locZip').value+'&pID='+$('pID').value;
	} else {
		$('locZip').setStyle('background','url(/img/locator_input_err.png) bottom right no-repeat');
	}
}

var sSearch = new Class({
	initialize: function() {
		this.running = false;
		this.searchClearBtm = '<br id="flexcroll-init" style="clear:both;"/>';
		this.Call = new Ajax('/php/search.php',{
			onRequest: function() {
				$('siteSearchStatusOuter').innerHTML='<img src="/img/nav_main/topnav/loading.gif" id="siteSearchStatus" />';
				$('siteSearchStatus').addEvent('click',function() { return false; });
				$('siteSearchResultsOuter').setStyle('opacity','0');
				$('siteSearchResultsOuter').setStyle('height','0');
				$('siteSearchResultsOuter').setStyle('z-index','2');
				$('siteSearchResults').innerHTML = '';
			},
			onComplete: function(tr) {
				$('siteSearchStatusOuter').innerHTML='<img src="/img/nav_main/topnav/clearSearch.gif" id="siteSearchStatus" />';
				$('siteSearchStatus').setStyle('cursor','pointer');
				$('siteSearchStatus').addEvent('click',function() {
					this.clearSearch();
				}.bind(this));
				$('siteSearchResultsOuter').setStyle('opacity','1');
				$('siteSearchResultsOuter').setStyle('height','500px');
				$('siteSearchResultsOuter').setStyle('z-index','50');
				$('siteSearchResults').innerHTML = tr+this.searchClearBtm;
				CSBfleXcroll('siteSearchResultsOuter');
			}.bind(this)
		});
		if ($chk($('siteSearch'))) {
			$('siteSearch').addEvents({
				'focus': function() {
					$('siteSearch').setStyle('color','#000');
					$('siteSearch').value = '';
				},
				'blur': function() {
					if($('siteSearch').value == '') {
						$('siteSearch').setStyle('color','#999');
						$('siteSearch').value = 'search';
					}
				},
				'keyup': function() {
					if($('siteSearch').value.length > 1) this.doSearch();
					if($('siteSearch').value.length == 0) {
						this.clearSearch();
						$('siteSearch').value = '';
						$('siteSearch').setStyle('color','#000');
					}
				}.bind(this)
			});
		}
	},
	doSearch: function() {
		if(this.running) this.Call.stop();
		this.Call.request('string='+$('siteSearch').value);
	},
	clearSearch: function() {
		$('siteSearchStatusOuter').innerHTML='<img src="/img/nav_main/topnav/blank.png" id="siteSearchStatus" />';
		$('siteSearchStatus').setStyle('cursor','default');
		$('siteSearch').setStyle('color','#999');
		$('siteSearch').value = 'search';
		$('siteSearchResultsOuter').setStyle('opacity','0');
		$('siteSearchResultsOuter').setStyle('height','0');
		$('siteSearchResultsOuter').setStyle('z-index','2');
		$('siteSearchResults').innerHTML = '';
		$('siteSearch').focus();
	}
});
