$(document).ready(function(){

	var prdImg = $("#productImageId");
	var prdId = prdImg.attr('name').split(':')[3];
	
	// find shop product id
	var shopPrdId = null;
	var hc=document.getElementsByTagName("head")[0].childNodes;
	for(var i=0;i<hc.length;i++){
		if(hc[i].nodeType===8){
			var comment=hc[i].nodeValue;
			if(comment.search(/SELECTION.+/)!=-1){
				var selection=hc[i].nodeValue.split("|");
				shopPrdId=selection[0].replace(/SELECTION/g,"").replace(/^\s*|\s*$/g,'');
			}
		}
	}

	// product image zoom lightbox
	$("#colorbox-zoom").colorbox({
		onOpen: function(){ 
			if (prdImg!=null) {
				var prdImgUrl2 = prdImg.attr('name'); /* img without dynamically created flag overlay */
				if (prdImgUrl2!=null) {
					prdImgUrl2 = prdImgUrl2.replace(/:/g, "/"); /* :dir:dir:image_2.jpg -> /dir/dir/image_2.jpg */
				} else {
					prdImgUrl2 = prdImg.attr('src'); /* default,  but img might be with dynamically created flag overlay */
				}
				var prdImgUrl3 = prdImgUrl2.replace(/(\d+)(\.[^.]+)$/,function(x,s,e){return (+s)+1+e;}); /* image_2.jpg -> image_3.jpg */
				/* check AJAX request on status code ==202 for _3 vs _2 img zoom level */
				$.ajax({
					url: prdImgUrl3,
					type: 'GET',
					async: false,
					complete: function(xhr, txtStatus) {
						if (xhr.status == 200) {
							$("#colorbox-zoom").colorbox({href: prdImgUrl3});
						} else {
							$("#colorbox-zoom").colorbox({href: prdImgUrl2});
						}
					}
				});
			}
		}
	});
	
	// video link lightbox
	$("#colorbox-video").colorbox({
		onOpen: function(){ 
			var videoPlayer = $("#swfID_0");
			if (videoPlayer.length>0) {
				$(this).colorbox({inline:true, href:"#swfID_0", innerWidth:"450px", innerHeight:"320px", scrolling: false});
			}
		}
	});

	// header bar and page logic for product img smooth galleries
	if (prdId!=null && Smoothgalleries[prdId]!=null) {
		handleSmoothgalleryExists(prdId);
	} else if (shopPrdId!=null && Smoothgalleries[shopPrdId]!=null) {
		handleSmoothgalleryExists(shopPrdId);
	}
	
	// header bar and page logic for product video
	$("#videoDiv").css('display', 'none');

	if($.browser.msie) {
		// IE work-around
		//$(window).load(function(){handleVideoPlayerExists();});
		$(window).load(function(){setTimeout(function(){handleVideoPlayerExists();}, 1250);});
	} else {
		$('#videoDiv').bind('DOMSubtreeModified',function(){
			handleVideoPlayerExists();
		});
	}

	function handleVideoPlayerExists(){
		if($("#swfID_0").length != 0 && window.location.pathname.indexOf('/schnaeppchen/') == -1) {
			$("#swfID_0").attr('wmode', 'transparent');
			$("#swfID_0").attr('autoplay', 'false');
			$("#colorbox-video").css('display', 'inline');
		}
	}
	
	function handleSmoothgalleryExists(prdId) {
		if (Smoothgalleries[prdId].pid && window.location.pathname.indexOf('/schnaeppchen/') == -1) {
			var galleryLink = $("#colorbox-gallery");
			galleryUrl = Smoothgalleries.url+Smoothgalleries[prdId].pid;
			galleryLink.attr('href', galleryUrl);
			galleryLink.css('display', 'inline');
			galleryLink.colorbox({iframe: true, innerWidth:"800px", innerHeight:"600px", scrolling: false});
		}
	}
	
});
