/*
* bookatonce Shop Text/Img Slideshow
* by Florian Ziemer
* http://www.bookatonce.com
*
* implements parts of Thickbox  3.1 by Cody Lindley
* http://codylindley.com
*
* The original Thickbox is licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*/

var Sh = {	initx   : false,
			init2   : 'nah',
			divTmp 	: '#detailsbox',		// Container holding text and imagedata for current module
			divImg 	: '#TB_ImageOff', 		// TB ImageDiv - where the content goes
			divL    : '#TB_load',			// "Loading..." image
			divW 	: '#TB_window', 		// TB Window - for repositioning
			divO 	: '#TB_overlay', 		// TB Overlay
			dTrigger: '.details a', 		// Elements that trigger the text TB
			iList 	: '.btgallery_toc a', 	// Div holding the image links
			iTrigger: '.btgallery_trigger', // Element(s) inside of the text TB that trigger the image TB
			src		: '.description',		// Subclass of dTrigger.parent.parent.parent that contains details
			loadImg : '/fileadmin/lib/images/shop/TB_load.gif',
			srcMod  : null,
			dWidth  : 700,
			tHeight : 60,
			ttHeight: 35,
			title   : "",
			dsGET   : -1,
			p2p		: -1,
			
			str     : {
				CLOSE_D_Y: '##CANCEL_Y##',
				CLOSE_D_N: '##CANCEL_N##',
				PREV   : '##PREV##',
				NEXT   : '##NEXT##',
				CLOSE_I: '##CLOSE##',
				IMAGE  : '##IMAGE##',
				TITLE  : '##TITLE##'
			},
			prevSrc : "",
			iIndex  : 0,
			iLen    : 0,
			ee      : null,
			dZ      : [0,0],
			preload : document.createElement('img'),
			init    : function() {
				if(Sh.initx === false) {
					Sh.initx = true;
					Sh.getStr();	
					$(Sh.dTrigger).unbind('click').click(function(){Sh.dShow(this);return false;});
					$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
					if(Sh.tb_detectMacXFF()){
						$(Sh.divO).addClass("TB_overlayMacFFBGHack").css('display','none').hide(); //use png overlay to hide flash
					}else{
						$(Sh.divO).addClass("TB_overlayBG").css('display','none').hide(); //use background and opacity
					}
					$("body").append("<div id='TB_load'><img src='"+Sh.loadImg+"' /></div>");
					Sh.dZ[0] = $(Sh.divO).css('z-index');
					Sh.dZ[1] = $(Sh.divW).css('z-index');
					var indx = (Sh.init2 === 'nah' ? 1 : 2);
					$('a.boMainButton,a.boAddOnButton').unbind().each(function(){
						$(this).click(function(){
							var link = $(this).attr('href');
							var form = $("form").eq(indx);
							form.attr('action',link);
							form.submit();
							return false;
						});
					});
					// overlay2overlay nav
					$('#TB_window a.external-link').live('click',function() {
						var linkid = $(this).attr('href').match(/[0-9]+$/);
						if(linkid.length>0) {
							Sh.p2p = linkid[0];
							$('table.shop').find('input.TB_chk').each(function() {
								if(''+$(this).val() == ''+Sh.p2p) {
									$(this).parentsUntil('table').find('.details a').click();
									return false;
								} //else $(this).parent('td').css('background','#f00 !important');
							});
						}
						return false;
					});
					// ENDOF overlay2overlay nav
					// dShow on GET param
					var urlParams = {};
					(function () {
					    var e,
					        d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); },
					        q = window.location.search.substring(1),
					        r = /([^&=]+)=?([^&]*)/g;

					    while (e = r.exec(q))
					       urlParams[d(e[1])] = d(e[2]);
					})();
					if(typeof urlParams.baoss_details != "undefined") Sh.dsGET = parseInt(urlParams.baoss_details);
					if(Sh.dsGET != -1) {
						$('table.shop').find('input.TB_chk').each(function() {
							if(''+$(this).val() == ''+Sh.dsGET) {
								Sh.dHide();
								$(this).parentsUntil('table').find('.details a').click();
								return false;
							} //else $(this).parent('td').css('background','#f00 !important');
						});
					}
					// ENDOF dShow on GET param
				}
			},
			dShow   : function(e) {
				if(e !== null) {
					e = $(e).parent().parent().find(Sh.src).eq(0);
					$(Sh.divTmp).html(e.html());
					$("html").css("overflow","hidden");
					Sh.str.TITLE = e.parent().children('.modname').html();
					Sh.ee = e;
				}
				
				$(Sh.divO).unbind('click').click(Sh.dHide);
				$(Sh.divO).css('z-index',Sh.dZ[0]).show();
				var hasbox = Sh.ee.parent().parent().find('.TB_chk').length > 0 ? true : false;
				$(Sh.divW).css('width',Sh.dWidth+'px').css('z-index',Sh.dZ[1]).html('<h1>'+Sh.str.TITLE+'</h1>'+$(Sh.divTmp).html()+'<br /><br /><div>'+(hasbox?'<input name="iey" type="button" class="TB_CANCEL_Y" value="'+Sh.str.CLOSE_D_Y+'" />':'')+'<input name="ien" type="button" class="TB_CANCEL_N" value="'+Sh.str.CLOSE_D_N+'" /></div>');
				
				$('.TB_CANCEL_N').unbind().click(function(){Sh.dHide(); return false;});
				$('.TB_CANCEL_Y').unbind().click(function(){
					Sh.ee.parent().parent().find('.TB_chk').attr('checked','checked');
					Sh.dHide();
					return false;
				});
				$(Sh.divW).find(Sh.iTrigger).click(Sh.iShow)
				Sh.dPos($(Sh.divW));
				$(Sh.divW).show();
				
				return false;
			},
			dHide   : function() {
				$(Sh.divO).css('z-index','-100').css('display','none').hide();
				$(Sh.divW).css('z-index','-100').css('display','none').hide();
				$(Sh.divL).hide();
				$("html").css("overflow","auto");
				return false;
			},
			iShow   : function() {
				Sh.iIndex = 0;
				Sh.iLen = $(Sh.divTmp+' '+Sh.iList).length;
				if(Sh.iLen > 0) {
					$(Sh.divO).unbind('click').click(Sh.iHide);
					$(Sh.divW).find(Sh.iTrigger).unbind('click');
					Sh.iNext(0);
				}
			},
			iHide   : function() {
				Sh.dShow(null);
				$(Sh.divL).hide();
				return false;
			},
			iNext   : function(offs) {
				if(offs<-1 || offs>1) return;
				Sh.iIndex = (Sh.iLen + Sh.iIndex + offs)%Sh.iLen;
				$(Sh.divW).hide();
				$(Sh.divL).show();
				$(Sh.preload).unbind('load').load(Sh.iNext2);
				var tar = $(Sh.divTmp+' '+Sh.iList).eq(Sh.iIndex).attr('href');
				if(tar == Sh.prevSrc) {
					Sh.preload.src = "ffs";
					Sh.preload.src = Sh.prevSrc;
				}else if(tar == "/uploads/tx_zebaoss/") {
					$(Sh.preload).unbind('load');
					Sh.prevSrc = "";
					Sh.iHide();
				} else {
					Sh.prevSrc = tar;
					Sh.preload.src=tar;
				}				
			},
			iNext2 : function() {
				if(Sh.preload.src != "ffs") {
					$(Sh.divW).html('<div id="TB_TITLE" style="height:'+Sh.ttHeight+';">'+Sh.str.TITLE+' <span>'+Sh.str.IMAGE+' '+(Sh.iIndex+1)+' / '+Sh.iLen+'</span></div><img src="'+Sh.preload.src+'" /><div id="TB_bottom" style="height:'+Sh.tHeight+'px;background-color:white;">hey</div>');
					$('#TB_bottom').html("<a id='TB_PREV' href='#'>&lt; "+Sh.str.PREV+"</a><a id='TB_NEXT' href='#'>"+Sh.str.NEXT+" &gt;</a><a id='TB_CLOSE' href='#'>"+Sh.str.CLOSE_I+"</a>");
					$('#TB_NEXT').click(function(){Sh.iNext(1); return false;});
					$('#TB_PREV').click(function(){Sh.iNext(-1); return false;});
					$('#TB_CLOSE').click(function(){Sh.iHide(); return false;});
					Sh.iPos($(Sh.divW));
					$(Sh.divL).hide();
					$(Sh.divW).show();
				}
			},
			getStr  : function() {
				var x = $('#cancelLink_Y');
				if(x.length>0) Sh.str.CLOSE_D_Y = x.eq(0).html();
				var x = $('#cancelLink_N');
				if(x.length>0) Sh.str.CLOSE_D_N = x.eq(0).html();
				x = $('#thickbox_next');
				if(x.length>0) Sh.str.NEXT = x.eq(0).html();
				x = $('#thickbox_prev');
				if(x.length>0) Sh.str.PREV = x.eq(0).html();
				x = $('#thickbox_close');
				if(x.length>0) Sh.str.CLOSE_I = x.eq(0).html();
				x = $('#thickbox_image');
				if(x.length>0) Sh.str.IMAGE = x.eq(0).html();
				x = $('#thickbox_btnInit2');
				if(x.length>0) Sh.init2 = 'ye';
			},
			tb_detectMacXFF : function() {
				var userAgent = navigator.userAgent.toLowerCase();
				if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
					return true;
				}
			},
			tb_getPageSize : function(){
				var de = document.documentElement;
				var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
				return h;
			},
			dPos    : function(elem) {
				elem.css('overflow','visible').css('height','auto');
				var TB_WIDTH = elem.width();
				var TB_HEIGHT = elem.height();
				var maxh = Sh.tb_getPageSize();
				if(maxh-TB_HEIGHT<60) {
					TB_HEIGHT = maxh-60;
				}
				
				//$(elem).css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px',width:(Sh.Width)});
				$(elem).css({marginLeft: '-' + parseInt((Sh.dWidth / 2),10) + 'px',width:(Sh.dWidth)+'px',height:(TB_HEIGHT)+'px',overflow:'auto'});
				if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) {
					$(elem).css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px',width:(Sh.dWidth)+'px',height:(TB_HEIGHT)+'px',overflow:'auto'});
				}
			},
			iPos   : function(elem) {
				elem.css('overflow','visible').css('height','auto');
				var TB_WIDTH = Sh.preload.width || $(Sh.divW).width();
				var TB_HEIGHT = Sh.preload.height || $(Sh.divW).height();
				TB_HEIGHT += Sh.tHeight + Sh.ttHeight ;
				if ( (!jQuery.browser.msie || jQuery.browser.version >= 7)) 
					TB_WIDTH += 12;
				$(elem).css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px',width:(TB_WIDTH)});
				if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) {
					$(elem).css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px',width:(TB_WIDTH)});
				}
			}
		};
if(typeof OM != "undefined") OM.addp(Sh.init);

