$(function() {
/*	$("a[rel*=lightbox]").lightBox();
	$("a[rel*=facebox]").each(function(){
		var href = $(this).attr("href");
		if(!href.match(/^#/))
			this.href += href.match(/\?/) ? "&popup=1" : "?popup=1";
		$(this).facebox();
	});
*/	$(".pngfix").ifixpng();
	$(".pngfix_str").ifixpng();
	$("a[rel*=hover] img")
		.each(function() { new Image().src = this.src.replace(/(\.[a-z]+)$/, "_s$1"); })
		.mouseover(function() { this.src = this.src.replace(/(\.[a-z]+)$/, "_s$1"); })
		.mouseout(function() { this.src = this.src.replace(/_s(\.[a-z]+)$/, "$1"); });
	$(".tabcontrol").tabs();
	$("input.datepicker").datepicker({
		dateFormat: "yy-mm-dd",
		showOn: "both",
		buttonImage: 'images/design_js/calendar-btn.png', 
		buttonImageOnly: true, 
		yearRange: "-90:+10" 
	}).attr("readonly", "readonly");
	$(".ui-datepicker-trigger").css({
		verticalAlign: 'middle',
		position: 'relative',
		left: '-20px'
	});
	
	if(!$('.newsletter .messageStackError').length && !$('.newsletter .messageStackSuccess').length)
		$('.newsletter .newsletter_box').css('display', 'none')
	$('.newsletter .newsletter_button img').click(function(){
		$('.newsletter .newsletter_box').slideToggle();
	});
	
	product_zoom();
});

function product_zoom() {
	var box = $('.prod_box_detail').css('position', 'relative');
    var img = $('.prod_image');
    if(!box[0] || !img[0]) return;
	$('<div id="prod_image_zoom"></div>').appendTo(box);
	$('<div id="prod_image_ovr"></div>').appendTo('body');
    $('#prod_image_zoom')
        .css({
            position: 'absolute',
            zIndex: '10',
            top: img.position().top + 'px',
            left: 20 + img.width() + 'px',
            width: box.width() - (img.width() + 30) + 'px',
            height: img.height() + 'px',
            overflow: 'hidden',
            border: '1px solid #000',
            background: '#fff',
            display: 'none'
        })
        .html('<img src="'+$('.prod_image img').attr('src')+'" border="0" alt="">');
    $('#prod_image_ovr').css({
        position: 'absolute',
        zIndex: '10',
        top: '0px',
        left: '0px',
        opacity: '0.5',
        border: '1px solid #000',
        background: '#777',
        display: 'none'
    });
    var off = $('.prod_image img').offset();
    var w = $('.prod_image img').width();
    var h = $('.prod_image img').height();
    var hide_timer = null;
    $('.prod_image img, #prod_image_ovr')
            .mouseover(function() {
                $('#prod_image_zoom').fadeIn(100);
                $('#prod_image_ovr').fadeIn(100);
                if(hide_timer) {
                        window.clearTimeout(hide_timer);
                        hide_timer = null;
                }
            })
            .mouseout(function() {
                hide_timer = window.setTimeout('$(\'#prod_image_zoom, #prod_image_ovr\').fadeOut(100);', 200);
            })
            .mousemove(function(e) {
            	//var s='';for(var i in e) s+=i+': '+(typeof(e[i]) == 'number' ? e[i] : typeof(e[i]))+'\n';alert(s);
                var div = $('#prod_image_zoom');
                var rx = div[0].scrollWidth / w;
                var ry = div[0].scrollHeight / h;
                var x = Math.max(0, (e.pageX - off.left) * rx - w / 2);
                var y = Math.max(0, (e.pageY - off.top) * ry - h / 2);
                div[0].scrollTop = y - div.height() / 4;
                div[0].scrollLeft = x - div.width() / 4;
                $('#prod_image_ovr').css({
                        width: Math.round(div.width() / rx) + 'px',
                        height: Math.round(div.height() / ry) + 'px',
                        top: Math.min(off.top + h - Math.round(div.height() / ry), Math.max(off.top, (e.pageY - Math.round(div.height() / ry) / 2))) - 1 + 'px',
                        left: Math.min(off.left + w - Math.round(div.width() / rx), Math.max(off.left, (e.pageX - Math.round(div.width() / rx) / 2))) - 1 + 'px'
                });
            });
}

function dlg_ajax(html, dlg, onsubmit) {
	if(html == '') {
		dlg.dialog('close');
		return;
	}
	
	dlg.html(html);
	$('.ui-dialog-close', dlg).click(function() {
		dlg.dialog('close');
		return false;
	});
	$('input.datepicker', dlg).datepicker({
		dateFormat: "yy-mm-dd",
		showOn: "both",
		buttonImage: 'images/design_js/calendar-btn.png', 
		buttonImageOnly: true, 
		yearRange: "-90:0",
		changeYear: true,
		changeMonth: true
	}).attr("readonly", "readonly");
	$('#ui-datepicker-div').addClass('promoteZ');
	$('form', dlg).ajaxForm({
		success: function(html) {
			dlg_ajax(html, dlg);
			if($.isFunction(onsubmit))
				onsubmit();
		}
	});
	$('.hspacer', dlg).height(400);
	$('.hspacer', dlg).height(392 + $(dlg).height() - $(dlg)[0].scrollHeight);
}

function a_page(page) {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'FullSpeed',
		width: 700,
		height: 500,
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/page.php', 'page='+escape(page), function(data) {
		dlg.dialog('option', 'title', data.title);
		dlg_ajax(data.body, dlg);
	}, 'json')
	return false;
}

