 $(document).ready(function() {

	var pv_timeout, img_timeout;

	// Start Lightbox
	$('a.lightbox').click(function(){

		return initLightbox($(this));
	});

	if($("#lb-blind").length > 0) {
		if($("#lb-blind").attr('href') != '') {
			return openLightbox($("#lb-blind").attr('href'), 658, 435, 100);
		}
	}

    // bind form using 'ajaxForm'
    if($('#shoppingcart').length > 0) {

		var shoppingcartOptions = {
			success:       showFormResponse  // post-submit callback
		};

		$('#shoppingcart').ajaxForm(shoppingcartOptions);
	}

	// Close Lightbox with ESC Key
	$(window).keydown(function(event){
	  switch (event.keyCode) {
		case 27:
				closeLightbox();
			break;
	  }
	});

	// Preview lightbox for product images
	$('img.lightbox').mouseenter(function(m) {

		var offset = $(this).offset();
		pv_class  = $(this).attr('id');
		pv_URL    = $(this).attr('longdesc');
		pv_top    = offset.top-119;
		pv_left   = offset.left-17;

		pv_timeout = window.setTimeout('openPreview(pv_class, pv_URL, pv_top, pv_left)', 100);
	});

	// Close lightbox on mouseout
	$('img.lightbox').mouseout(function() {
		pv_timeout = window.setTimeout('closePreview()', 1);
	});

	// Clear close timeout
	$('div.previewwrapper').live('mouseover', function(){
		window.clearTimeout(pv_timeout);
	});

});

function initLightbox(element) {
	var lb_URL    = element.attr('href');
		var lb_width;
		var lb_height;
		var lb_top    = 100;

		var obj = getLightboxParams(element.attr('rel'));

		// set lightbox dimension by mode
		switch(obj.mode) {
			case 'buyer':
					lb_width  = '415';
					lb_height = '467';
				break;
			case 'shoppingcart':
					lb_width  = '658';
					lb_height = '435';
				break;
			case 'product':
					lb_width  = '177';
					lb_height = '124';
				break;
			default:
					lb_width  = '766';
					lb_height = '501';
				break;
		}

		// overwrite lightbox dimensions
		if(obj.width) {
			lb_width  = obj.width;
		}

		if(obj.height) {
			lb_height  = obj.height;
		}

		if(obj.top) {
			lb_top  = obj.top;
		}

	return openLightbox(lb_URL, lb_width, lb_height, lb_top);
}

function showFormResponse(result, status) {
	return openLightbox(result, 658, 435, 100);
}

// process rel attribute
function getLightboxParams(rel) {

	box = {
		regex: {
			mode: /^(light|product|shoppingcart|buyer)box(.*?)/i, // rel attribute format for lightbox mode
			param: /\s*([a-z_]*?)\s*=\s*(.+)\s*/   // rel string parameter
		}
	};

	obj = {
		mode:    'lightbox'
	};

	if(rel) {

		var m = rel.match(box.regex.mode);

		if(m) {
			obj.mode = escape(m[1]);
		}

		var params = rel.split(';');

		// other parameters
		for (var i = 0; i < params.length; i++) {
			m = params[i].match(box.regex.param);

			if(m){
				obj[m[1]] = m[2];
			}
		}
	}

	return obj;
}

/* Open Lightbox */
function openLightbox(lb_URL, lb_width, lb_height, lb_top) {

	var lb_middle    = ($('html').width() / 2);
	var lb_bg_width  = Math.round(lb_width/2);
	lb_middle        = lb_middle - lb_bg_width;
	var lb_if_width  = lb_width-12;
	var lb_if_height = lb_height-10;

	$('body').append('<div id="lb-background"></div>');
	$('body').append('\
			<div class="iframewrapper" style="top: 0px; left: 0px; height:10px; width: 0px;">\n\
				<div class="lb-topleft" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
				<div class="lb-topright" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
				<a href="#" class="i-close">schliessen</a>\n\
				<div class="lb-middleleft" style="height:' + lb_if_height + 'px;">\n\
					<div class="lb-middleright" style="height:' + lb_if_height + 'px;">\n\
						<iframe id="lb-content" src="' + lb_URL + '" width="' + lb_if_width + '" height="' + lb_if_height + '" frameborder="0"></iframe>\n\
					</div>\n\
				</div>\n\
				<div class="lb-bottomleft" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
				<div class="lb-bottomright" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
			</div>');

	// hide first to show later
	$('div.iframewrapper').hide();
	$('div.iframewrapper').css('width', lb_width+'px');
	$('div.iframewrapper').css('height', lb_height+'px');
	$('div.iframewrapper').css('left', lb_middle);
	$('div.iframewrapper').css('top', lb_top);
	$('div#lb-background').css('opacity', 0);
	$('div.iframewrapper').show();

    $('div.iframewrapper').css('zIndex', 10010);

	$('.iframewrapper a.i-close').click(closeLightbox);

	return false;
}

/* Open Message Lightbox */
function openMessageLightbox(msg, lb_width, lb_height, lb_top) {

	var lb_middle    = ($('html').width() / 2);
	var lb_bg_width  = Math.round(lb_width/2);
	lb_middle        = lb_middle - lb_bg_width;
	var lb_if_height = lb_height-10;

	$('body').append('<div id="lb-background"></div>');
	$('body').append('\
			<div class="iframewrapper" style="top: 0px; left: 0px; height:10px; width: 0px;">\n\
				<div class="lb-topleft" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
				<div class="lb-topright" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
				<a href="#" class="i-close">schliessen</a>\n\
				<div class="lb-middleleft" style="height:' + lb_if_height + 'px; background-color:white;">\n\
					<div class="lb-middleright" style="height:' + lb_if_height + 'px; padding:50px;">'+msg+'\n\
					</div>\n\
				</div>\n\
				<div class="lb-bottomleft" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
				<div class="lb-bottomright" style="width:' + lb_bg_width + 'px;">&nbsp;</div>\n\
			</div>');

	// hide layer first to show later
	$('div.iframewrapper').hide();
	$('div.iframewrapper').css('width', lb_width+'px');
	$('div.iframewrapper').css('height', lb_height+'px');
	$('div.iframewrapper').css('left', lb_middle);
	$('div.iframewrapper').css('top', lb_top);
	$('div.iframewrapper').show();

    $('div.iframewrapper').css('zIndex', 10010);

	$('.iframewrapper a.i-close').click(closeLightbox);

	return false;
}

/* Close Lightbox */
function closeLightbox() {

	if($('#lb-background').length > 0) {
		$('.iframewrapper').fadeOut(400, function(){
			$(this).remove();
		});

		$('#lb-background').fadeTo(400,0,function(){
			$(this).remove();
		});
	}

	return false;
}

/* Open product preview */
function openPreview(pv_id, pv_URL, pv_top, pv_left, pv_width, pv_height) {

	if (!pv_width ) pv_width  = '220';
	if (!pv_height) pv_height = '124';
	var pv_innerHeight = pv_height - 10;
	var pv_bg_width  = Math.round(pv_width/2);

	$.ajax({
		type: "GET",
		dataType: "html",
		url: pv_URL,
		beforeSend: function(){
		},
		cache: true,
		success: function(pr_detail){

			if($('div.previewwrapper').length > 0) {
				$('div.previewwrapper').remove();
			}
			$('body').append('\
				<div class="previewwrapper lb_' + pv_id + '" style="top: 0px; left: 0px; height:10px; width: 0px;">\n\
					<div class="lb-topleft" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
					<div class="lb-topright" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
					<div class="lb-middleleft" style="width:' + pv_width + 'px; height: ' + pv_innerHeight + 'px;">\n\
						<div class="lb-middleright" style="width:' + pv_width + 'px; height: ' + pv_innerHeight + 'px;">\n\
							'+
								pr_detail
							+'\n\
						</div>\n\
					</div>\n\
					<div class="lb-bottomleft" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
					<div class="lb-bottomright" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
				</div>'
			);

			// hide layer first to show later
			$('div.previewwrapper').hide();
			$('div.previewwrapper').css('width', pv_width+'px');
			$('div.previewwrapper').css('height', pv_height+'px');
			$('div.previewwrapper').css('left', pv_left);
			$('div.previewwrapper').css('top', pv_top);
			$('div.previewwrapper').show();

            $('div.previewwrapper').css('zIndex', 10010);

			// Clear timeout from flex actions class if set
			$('div.previewwrapper').bind('mouseover', function(){
				window.clearTimeout(flexFunctions.productDetailsClosingTimeout);
			});

			$('div.previewwrapper').bind('mouseleave', function(){
				window.setTimeout('closePreview()', 3);
			});
		}
	});

	return false;

}


/* Open prebid layer */
function openLocalMessageLightbox(msg, pv_id, pv_URL, pv_top, pv_left, pv_width, pv_height) {
    //alert(msg+"  "+pv_id+"  "+pv_URL+"  "+pv_top+"  "+pv_left+"  "+pv_width+"  "+pv_height);
	if (!pv_width ) pv_width  = '220';
	if (!pv_height) pv_height = '124';
	var pv_innerHeight = pv_height - 10;
	var pv_bg_width  = Math.round(pv_width/2);

	$.ajax({
		type: "POST",
		dataType: "html",
        data: "msg="+msg+"&productId="+pv_id,
        url: pv_URL,
		beforeSend: function(){
		},
		cache: true,
		success: function(content){

			if($('div.previewwrapper').length > 0) {
				$('div.previewwrapper').remove();
			}
			$('body').append('\
                <div class="previewwrapper lb_voteMsg" style="top: 0px; left: 0px;  height:10px; width: 0px;">\n\
                    <div class="lb-topleft" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
                    <div class="lb-topright" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
                    <a href="#" class="i-close">schliessen</a>\n\
                    <div class="lb-middleleft" style="width:' + pv_width + 'px; height: ' + pv_innerHeight + 'px; background-color:white;">\n\
                        <div class="lb-middleright" style="width:' + pv_width + 'px; height: ' + pv_innerHeight + 'px;">\n\\'
                            + content +
                        '</div>\n\
                    </div>\n\
                    <div class="lb-bottomleft" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
                    <div class="lb-bottomright" style="width:' + pv_bg_width + 'px;">&nbsp;</div>\n\
                </div>'
			);
			$('div.previewwrapper').show();
			$('div.previewwrapper').css('width', pv_width+'px');
			$('div.previewwrapper').css('height', pv_height+'px');
			$('div.previewwrapper').css('left', pv_left);
			$('div.previewwrapper').css('top', pv_top);
			$('div.previewwrapper a.i-close').click(closePreview);

            $('div.previewwrapper').css('zIndex', 10010);

		}
	});

	return false;


}


/* Close Lightbox */
function closePreview() {

	$('.previewwrapper').hide();
	$('.previewwrapper').remove();

	return false;
}
