/* ******************************************************************

	prd2fr_code.js

****************************************************************** */


var prdfrPreloader =
		"<table cellspacing='0' cellpadding='0' border='0' width='100%' height='100%' >" +
		"<tr><td align='center' valign='middle' >" +
		"	<img src='/img/layout/loading.gif' alt='image' />" +
		"</td></tr>" +
		"</table>";


function LoadProductList( rid, cat, act_cat, gindex )
{
		var lscontent = $(".js_productfrbox_list:first");
		lscontent.empty();
		lscontent.append( prdfrPreloader );	// preloader start.
		$.ajax({
			async: true,
			type: "GET",
			url: "/page.php?cat=" + act_cat + (rid?"&rid=" + rid:"") + "&prodcat=" + cat + "&gindex=" + gindex,	//"cat=" jest dla aktualizera, "prodcat=" jest dla modulu.
		//	url: "/page.php?cat=" + act_cat + "&prodcat=" + cat + "&gindex=" + gindex,	//"cat=" jest dla aktualizera, "prodcat=" jest dla modulu.
			dataType: "html",
			success: function( html, textStatus ){
				var elem = $(".js_productfrbox_list:first");
				elem.empty();
				elem.append( html );
				// setup events.
				elem.find(".js_showproductframebox").click(function(){
					var rid_ = $(this).attr("rid");
					var cat_ = $(this).attr("cat");
					if(rid_){
						LoadFrameProductContent( rid_, cat_ );
					}
				});
				// setup next/prev buttons.
				elem.find(".js_showproduct_next_list").click(function(){
					var cat_ = $(this).attr("cat");
					var gindex_ = $(this).attr("gindex");
					if(gindex_){
						LoadProductList( 0, cat_, act_cat, gindex_ );
					}
				});
				elem.find(".js_showproduct_prev_list").click(function(){
					var cat_ = $(this).attr("cat");
					var gindex_ = $(this).attr("gindex");
					if(gindex_){
						LoadProductList( 0, cat_, act_cat, gindex_ );
					}
				});
			}
		});
}


// funkcja ladujaca content kiedy dany jest rid.
function LoadFrameProductContent( rid, cat )
{
		var lfcontent = $(".js_productfrboxcontent:first" );
		lfcontent.empty();
		lfcontent.append( prdfrPreloader );	// preloader start.
		$.ajax({
			async: true,
			type: "GET",
			url: "/page.php?cat=99&rid=" + rid + "&prodcat=" + cat,
			dataType: "html",
			success: function( html, textStatus ){
				var lfcontent = $(".js_productfrboxcontent:first" );
				lfcontent.empty();		// preloader stop.
				lfcontent.append( html );
				var statusbox = lfcontent.find(".js_statusbox");
				SetupCartAhrefEvents( lfcontent, function(){
					//statusbox.text( "...." );
					statusbox.html( prdfrPreloader );
					if( statusbox.myTimerId ){
						clearTimeout( statusbox.myTimerId );
						statusbox.myTimerId = 0;
					}
				}, function( statusobj ){
					statusbox.html( statusobj.addresult? statusobj.tpl_obj.msg_cartaddok: statusobj.tpl_obj.msg_cartadderror );
					if( statusbox.myTimerId ){
						clearTimeout( statusbox.myTimerId );
						statusbox.myTimerId = 0;
					}
					statusbox.myTimerId = setTimeout( function(){
						statusbox.myTimerId = 0;
						statusbox.html( "&nb"+"sp;" );
					}, 3000 );
				});
				SetupZalOnlyButtonsDlaFirm( lfcontent );
			}
		});
}


function SetupPagePrd2frViews( parentElements )
{
	// wybra elementy ze strony otwierajace ramke przegladania produktow.
	$( ".js_showproductframebox", parentElements ).click( function(){
		var dlgbox2a = $(".js_productfrbox");
		dlgbox2a.css( "display", "block" );
		dlgbox2a.dialog("open");
		$(".ui-dialog-content[display='block']").css( "height", "auto" );

		var jsonstr = $(this).attr("params");
		var jsonobj = eval( "(" + jsonstr + ")" );
		LoadFrameProductContent( jsonobj.rid, jsonobj.cat );
		LoadProductList( jsonobj.rid, jsonobj.cat, jsonobj.act_cat, "" );

		//SetupZalOnlyButtonsDlaFirm();
		//alert("a");
	});
}


$(document).ready( function(){
	// inicjuje dialog box.
	var dlgbox2 = $(".js_productfrbox:first").eq(0);
	if( dlgbox2 != undefined && dlgbox2.length ){
		var bhgt = $("body").height();
		bhgt += 500;
		dlgbox2.dialog({
			modal: true,
			autoOpen: false,
			overlay: {
				opacity: 0.7,
				width: "100%",
				height: bhgt + "px",
				backgroundColor: "#393939"
			},
			resizable: false,
			width: "auto",
			height: "auto"
		});
		dlgbox2.find(".js_closebutton_productfrbox").click(function(){
			dlgbox2.dialog("close");
		});
	}
	SetupPagePrd2frViews( $("body") );
});

