
/*Get Height of Body */
$.getDocHeight = function() {
    var D = document;
    return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};
 

/*--- slide gallery 1 ---*/
function initGall1(){
	var stay_time = 5000; //in ms or 'false' if not needed
	var change_speed = 800; //in ms
	
	var _hold = $('#slede-box');
	if(_hold.length){
		var _f = true;
		var _t;
		var list_hold = _hold.find('ul.gallery-list');
		var _list = list_hold.children();
		if(_list.length > 1){
			var _a = _list.index(_list.filter('.active:eq(0)'));
			if(_a == -1) _a = _list.length - 1;
			var _step = list_hold.parent().width();
			var _btn = $('<ul class="switcher"></ul>');
			for(var i = 0; i < _list.length; i++){
				_btn.append('<li><a href="#">'+(i+1)+'</a></li>');
			}
			_hold.append(_btn);
			_btn = _btn.find('a');

			_list.removeClass('active').eq(_a).addClass('active');
			_btn.removeClass('active').eq(_a).addClass('active');
			list_hold.css('left', -_step*_a);
			
			_hold.mouseenter(function(){
				_f = false;
				if(_t) clearTimeout(_t);
			}).mouseleave(function(){
				_f = true;
				if(_t) clearTimeout(_t);
				if(_f && stay_time){
					_t = setTimeout(function(){
						if(_a < _list.length - 1) moveList(_a + 1);
						else moveList(0);
					}, stay_time);
				}
			});
			_btn.click(function(){
				moveList(_btn.index(this));
				return false;
			});
			
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) moveList(_a + 1);
					else moveList(0);
				}, stay_time);
			}
		}
		function moveList(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active');
				_list.eq(_ind).addClass('active');
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				list_hold.animate({left: -_step*_ind},{easing: 'easeOutCubic', queue:false, duration:change_speed}); //you can select easing: easeOutQuart or easeOutExpo
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) moveList(_a + 1);
					else moveList(0);
				}, stay_time+change_speed);
			}
		}
	}
}
/*--- slide gallery 2 ---*/
function initGall2(){
	var _speed = 500; //in ms
	$('div.slide-gallery').each(function(){
		var _hold = $(this);
		var btn_prev = _hold.find('a.link-prev');
		var btn_next = _hold.find('a.link-next');
		var list_hold = _hold.find('div.slide-content > ul');
		var hold_w = list_hold.parent().width();
		var list_w = list_hold.children().outerWidth()*list_hold.children().length;
		var _step = hold_w;
		
		btn_prev.click(function(){
			moveList(false);
			return false;
		});
		btn_next.click(function(){
			moveList(true);
			return false;
		});
		var _m = 0;
		list_hold.css('left', -_m);
		function moveList(_f){
			if(_f){
				if(list_w - _m > hold_w) _m += _step;
				else _m = 0;
			}
			else{
				if(_m > 0) _m -= _step;
				else _m = Math.ceil((list_w -hold_w)/_step)*_step;
			}
			list_hold.animate({left:-_m},{queue:false, duration: _speed});
		}
	});
}
/*--- fade gallery 3 ---*/
function initGall3(){
	var stay_time = 5000; //in ms or 'false' if not needed
	var change_speed = 600;
	$('div.gallery-l3').each(function(){
		var ajax_f = false;
		var _f = true;
		var _t;
		var _hold = $(this);
		var _list = [];
		_hold.find('ul.src li').each(function(){
			var t_el = [];
			t_el._src = this.innerHTML;
			t_el._box = false;
			_list.push(t_el);
		});
		var img_hold = _hold.find('div.gallery-list');
		var _a = 0;
		var _loader = $('<div class="loader">Loading...</div>');
		_loader.hide();
		img_hold.html(_loader);
		_hold.append('<div class="switcher-holder"></div>');
		//alert(_list[_a]._src);
		$.ajax({
			url: _list[_a]._src,
			dataType: 'html',
			success: function(_html){
			    //alert(_html);
				_list[_a]._box = $(_html);
				_list[_a]._box.addClass('active');
				img_hold.append(_list[_a]._box);
				ajax_f = true;
			}/*, 
                error:function (xhr, ajaxOptions, thrownError){ 
                    alert(xhr.status); 
                    alert(thrownError); 
                    }*/ 
		});
		if(_list.length > 1){
			var _btn = $('<ul class="switcher"></ul>');
			for(var i = 0; i < _list.length; i++){
				_btn.append('<li><a href="#">'+(i+1)+'</a></li>');
}
            //_hold.find('div.switcher-holder').append(_btn).append('<a class="some-link" href="#">VIEW SLIDESHOW</a>');
			_hold.find('div.switcher-holder').append(_btn).append('<a class="some-link" href="#"></a>');
			_btn = _btn.find('a');
			_btn.eq(_a).addClass('active');
			_btn.click(function(){
				changeEl(_btn.index(this));
				return false;
			});
			_hold.find('div.switcher-holder a.some-link').click(function(){
				if(_f){
					_f = false;
					if(_t) clearTimeout(_t);
				}
				else{
					_f = true;
					if(_t) clearTimeout(_t);
					if(_f && stay_time){
						_t = setTimeout(function(){
							if(_a < _list.length - 1) changeEl(_a + 1);
							else changeEl(0);
						}, stay_time/2);
					}
				}
				return false;
			});
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_a != _ind && ajax_f){
				ajax_f = false;
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				if($.browser.msie) _list[_a]._box.removeClass('active').hide();
				else _list[_a]._box.removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
				if(_list[_ind]._box){
					if($.browser.msie) _list[_ind]._box.addClass('active').show();
					else _list[_ind]._box.addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
					_a = _ind;
					ajax_f = true;
				}
				else{
					_loader.show();
					$.ajax({
						url: _list[_ind]._src,
						dataType: 'html',
						success: function(_html){
			                //alert(_html);
							_list[_ind]._box = $(_html);
							_list[_ind]._box.find('img').load(function(){
								if($.browser.msie) _list[_ind]._box.addClass('active').show();
								else _list[_ind]._box.addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
								_loader.hide();
								_a = _ind;
								ajax_f = true;
							});
							if($.browser.msie) _list[_ind]._box.hide();
							else _list[_ind]._box.css('opacity', 0);
							img_hold.append(_list[_ind]._box);
						}
					});
				}
			}
			if(_f && stay_time){
				if(_t) clearTimeout(_t);
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}
/*--- accordion function ---*/
function initAccordion(){
	var _speed = 300; //in ms
	$('.accordion').each(function(){
		var _list = $(this).children();
		var _a = _list.index(_list.filter('.active:eq(0)'));
		_list.removeClass('active');
		if(_a != -1) _list.eq(_a).addClass('active');
		_list.each(function(_i){
			var _el = $(this);
			this._btn = _el.children('a.opener');
			this._box = _el.children('div.slide');
			this._h = this._box.height();
			if(_i == _a) this._box.show();
			else this._box.hide();
			this._btn.click(function(){
				if(_a != -1){
					_list.eq(_a).removeClass('active');
					_list.get(_a)._box.stop().animate({height: 0}, _speed, function(){
						$(this).css({display: 'none', height: 'auto'});
					});
				}
				if(_a != _i){
					_list.eq(_i).addClass('active');
					if(_list.get(_i)._box.is(':hidden')){
						_list.get(_i)._box.show();
						_list.get(_i)._h = _list.get(_i)._box.height();
						_list.get(_i)._box.height(0);
					}
					_list.get(_i)._box.stop().animate({ height: _list.get(_i)._h}, _speed, function(){
						$(this).height('auto');
					});
					_a = _i;
				}
				else{
					_a = -1;
				}
				return false;
			});
		});
	});
}
/*--- register popup ---*/
function registerPopup() {    
	var _hold = $('#registered');	
	if(_hold.get(0))
	{
		_hold.get(0)._f = 485;	
	}
	if(_hold.length){
		var box_hold = _hold.find('div.popup-hold');
		var _box = $('<iframe scrolling="no" hspace="0" vspace="0" frameborder="0" id="register-popup" name="register-popup" allowTransparency="true" ></iframe>');
		_box.height(0);
		box_hold.hide().append(_box);
		_hold.get(0)._f =-1;	
		 //_hold.get(0)._f = 485;
		//_box.attr('src', this.href);
		_hold.find('div.column a').click(function(){					
			if(_hold.get(0)._f != 0 && _hold.get(0)._f != -1){				
				if(_hold.hasClass('opened')){
					_hold.removeClass('opened');
					_box.stop().animate({height: 0}, 485, function(){ box_hold.hide();});
				}
				else{
					_hold.addClass('opened');
					if(box_hold.is(':hidden')){
						_box.height(0);
						box_hold.show();
					}
					_box.stop().animate({height: _hold.get(0)._f}, 485);
				}
			}
			else{			
				_hold.get(0)._f = 485;
				_box.attr('src', this.href);
			}
			return false;
		});		 
		_hold.find('div.column a').click();		
	}
}
/*--- subscribe popup---*/
function subscribePopup() {    
	var _box = $('<iframe scrolling="no" hspace="0" vspace="0" frameborder="0" name="subscribe-popup" allowTransparency="true" id="subscribe-popup"></iframe>');
	if($.browser.msie) _box.hide();
	else _box.css({display:'none', opacity: 0});
	$('#wrapper').append(_box);
	var _btn = $('div.convention-holder a.subscribe-btn');
	_btn.click(function(){
		if($(this).hasClass('active')){
			$(this).removeClass('active');
			if($.browser.msie) _box.hide();
			else _box.stop().animate({opacity:0}, 400, function(){_box.hide();});
		}
		else{
			_btn.removeClass('active');
			$(this).addClass('active');
			_box.attr('src', this.href);
		}
		return false;
	});
}
/*--- images popup ---*/
function initPopus(){
	if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
	var _fader = $('#fader');
	_fader.hide();
	var _popup = -1;
	
	$('a.with-popup').each(function(){	    
		var _el = this;
		if(_el.hash && _el.hash.length > 1){
			_el._popup = $(_el.hash);
			if(_el._popup.length){
				_el.onclick = function(){
					if(_el._popup){
						_popup = _el._popup;							
						$(".popup-flash").find("img").attr("src",$(this).find("img").attr("src"));	
						showPopup();
					}
					return false;
				}
				_el._popup.find('a.close').click(function(){
					if($.browser.msie){
						_popup.hide();
						_fader.fadeOut(200, function(){ _popup = -1;});
						if($.browser.version < 7) $('#wrapper select').css('visibility', 'visible');
					}
					else{
						_popup.fadeOut(400, function(){
							_fader.fadeOut(200);
							_popup = -1;
						});
					}
					return false;
				});
			}
		}
	});
	_fader.click(function(){
		if(_popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_fader.fadeOut(200, function(){ _popup = -1;});
				if($.browser.version < 7) $('#wrapper select').css('visibility', 'visible');
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
		return false;
	});
	
	$(document).keydown(function(e){
		if(!e)evt = window.event;
		if(e.keyCode == 27 && _popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200, function(){ _popup = -1;});
				if($.browser.version < 7) $('#wrapper select').css('visibility', 'visible');
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
	});
	function showPopup(){
		_fader.css({
			opacity: 0,
			height: initH(),
			display:'block'
		});
		_popup.css({top: $(window).scrollTop()});
		if($.browser.msie && $.browser.version < 7) $('#wrapper select').css('visibility', 'hidden');
		_fader.fadeTo(400, 0.5, function(){
			if(_popup != -1){
				if($.browser.msie) _popup.show();
				else _popup.fadeIn(200, function(){
					if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
				});
				_popup.css({top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2});
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
			}
		});
	}
	function initH(){
		var _h = $('#wrapper').outerHeight();
		if(_h < $(window).height()) _h = $(window).height();
		if(_h < $('body').height()) _h = $('body').height();
		return _h;
	}
}
/*---- clear inputs ---*/
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		var _el = $(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
});

$(".payTermsClass").unbind("focus");
$(".payTermsClass").unbind("blur");
}


/*--- iframe ---*/
function initIframe() {
    /*    
	if(window.name == 'register-popup'){
		var _hold = $(window.parent.document).find('#registered');
		var box_hold = _hold.find('div.popup-hold');
		var _box = box_hold.find('iframe');
		_box.css('height', 0);
		box_hold.show();
		_hold.get(0)._f = $('div.popup:eq(0)').outerHeight(true);
		_hold.addClass('opened');
		_box.animate({height: _hold.get(0)._f}, 400);
		$('input.button').bind('click', function(){
			_hold.removeClass('opened');
			_box.stop().animate({height: 0}, 400, function(){ box_hold.hide();});
			return false;				// temp, must delete
		});
	}
	*/
	if(window.name == 'subscribe-popup'){
		var a_btn = $(window.parent.document).find('div.convention-holder a.subscribe-btn').filter('.active');
		var _box = $(window.parent.document).find('#subscribe-popup');
		if($.browser.msie) _box.show();
		else _box.css({opacity:0, display:'block'});
		_box.height($('div.subscribe-box:eq(0)').outerHeight());
		_box.css({
			top: a_btn.offset().top - $(window.parent.document).find('#wrapper').offset().top - _box.outerHeight(),
			left: a_btn.offset().left - $(window.parent.document).find('#wrapper').offset().left
		});
		if($.browser.msie) _box.show();
		else _box.animate({opacity: 1}, 400);
		$('a.close').click(function(){
			a_btn.removeClass('active');
			if($.browser.msie) _box.hide();
			else _box.stop().animate({opacity: 0}, 400, function(){ _box.hide();});
			return false;
		});
		/*$('input.button').click(function(){
			if($.browser.msie) _box.hide();
			else _box.stop().animate({opacity: 0}, 400, function(){ _box.hide();});
			a_btn.removeClass('active');	// temp, must delete
			return false;					// temp, must delete
		});*/
	}
}
if (window.addEventListener) window.addEventListener("load", initIframe, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initIframe);

function noError() { return true; }
window.onerror = noError;


$(document).ready(function() {

    if ($(".popup-hold").css("height") != undefined) {
        $("#content h1").css("margin-top", "90px");
        $(".item-about-site").css("margin-top", "90px");
    }
    else {
        $("#content h1").css("margin-top", "0px");
        $(".item-about-site").css("margin-top", "0px");
    }

    var h = 0;
    $(".product").each(function() {
        var th = $(this).height();
        if (h < th) {
            h = th;
        }
    });
    $(".product").height(h);
    //alert('I am about to load everything!!!');
    initGall1();
    initGall2();
    initGall3();
    initAccordion();
    $('input:radio').customRadio();
    //registerPopup();
    subscribePopup();
    initPopus();
    clearInputs();

    /* for events and signage page - login refresh */
    var loc = String($(window).attr("location")).toLowerCase();
    var parloc = String($(parent.window).attr("location")).toLowerCase();


    if ((loc.indexOf("show.aspx") != -1 || loc.indexOf("signage-review.aspx") != -1) && (parloc.indexOf("show.aspx") != -1 || parloc.indexOf("signage-review.aspx") != -1) && window != parent.window) {
        parent.window.location = parent.window.location;
    }

    $("input").each(function() {
        if ($(this).attr("id").indexOf("chkRememberMe") != -1) {
            $(this).css("float", "left");
        }
    });

    //sets display to block if login failed
    if ($(".errorrow").html()) {
        if ($(".errorrow").html().trim() != "") {
            $(".popup-hold").css("display", "block");
        }
    };
    if ($(".registrationerror").html()) {
        if ($(".registrationerror").html().trim() != "") {
            $(".popup-hold").css("display", "block");
        }
    };

    showMacMessage();

});

/*--- custom radio's ---*/

jQuery.fn.customRadio = function(_options) {
    var _options = jQuery.extend({
        radioStructure: '<div></div>',
        radioDisabled: 'disabled',
        radioDefault: 'radioArea',
        radioChecked: 'radioAreaChecked'
    }, _options);

    return this.each(function() {
        var radio = jQuery(this);        
        if (!radio.hasClass('outtaHere') && radio.is(':radio')) {
            var replaced = jQuery(_options.radioStructure);
            this._replaced = replaced;
            if (radio.is(':disabled')) replaced.addClass(_options.radioDisabled);
            else if (radio.is(':checked')) replaced.addClass(_options.radioChecked);
            else replaced.addClass(_options.radioDefault);
            replaced.click(function() {
                if ($(this).hasClass(_options.radioDefault)) {
                    radio.change();
                    radio.attr('checked', 'checked');
                    changeRadio(radio.get(0));
                }
            });
            radio.click(function() {
                changeRadio(this);
            });
            replaced.insertBefore(radio);
            radio.addClass('outtaHere');
        }
    });
    function changeRadio(_this) {
        $('input:radio[name=' + $(_this).attr("name") + ']').not(_this).each(function() {
            if (this._replaced && !$(this).is(':disabled')) this._replaced.removeClass().addClass(_options.radioDefault);
        });
        _this._replaced.removeClass().addClass(_options.radioChecked);
    }
}

function ShowRegisterLoginPopup() {   
    if ($(".popup-hold").css("display") == "none") {
        $(".popup-hold").slideDown();                
    } 
    else {
        $(".popup-hold").slideUp();
    }
}
var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

//$(document).ready(showMacMessage);

function showMacMessage() {
    if (BrowserDetect.OS == "Mac") {
        $('#macMessage').text("Please note that the PDF document may open in a new window using Mac’s built-in PREVIEW application. If this is the case, please review the signage in the new window, and then leave your comments using the “Comment” button below on this page.");
    }
}

 $(document).ready(function() {
		$('.order-now-btn').click(function()
			{
				$('.ajax-spinner').fadeIn(500);
			});
 });




