Ext.onReady(function(){
	Ext.QuickTips.init();

	// create the Data Store
	var store = new Ext.data.JsonStore({
		proxy: new Ext.data.HttpProxy({url: './admin/dataminer.php?request=coupons'}),
		root: 'pictures',
		totalProperty: 'totalCount',
		fields: ['col0', 'col1', 'col2'],
		remoteSort: true,
		autoLoad: {params:{start:0, limit:3}}
	});
	
	buildDom = function (src) {
		var frame = window.frames[0];
		var path = src.substring(0, src.lastIndexOf('/') + 1);
		var filename = src.substring(src.lastIndexOf('/') + 1);
		filename = filename.split('_');
		filename = filename[1] + '_' + filename[2];
		src = path + filename;
		frame.document.body.innerHTML = '	<img alt="" style="" src="' + src.replace(/small.jpg/, 'big.jpg') + '" />';
	}

	printIframe = function(src) {
		var frame = window.frames[0];
		if (navigator.userAgent.indexOf("Opera")!=-1) {
			var path = src.substring(0, src.lastIndexOf('/') + 1);
			var filename = src.substring(src.lastIndexOf('/') + 1);
			filename = filename.split('_');
			filename = filename[1] + '_' + filename[2];
			src = path + filename;
			var owin=window.open(frame.location + '?src=' + src);
			owin.print();
		} else {
			try {
				frame.document.focus();
				frame.document.execCommand('print', false, null);
			}
			catch(e) {
				frame.focus();
				frame.print(); 
			}
		} 
	}
	
	clickOperation = function (src) {
		var mainwin = window.location;
		Ext.Ajax.request({
			url: './admin/dataminer.php?request=couponkeywordsearch',
			params: {
						coupon_keyword: src.substring(src.lastIndexOf('/') + 1, src.indexOf('_small.jpg'))
					},
			method: 'POST',
			callback: function (options, success, response) {
				// Visszateresi ertek fogadasa
				var resultobj	= Ext.util.JSON.decode(response.responseText);
				if (resultobj.id) {
					var window = new Ext.Window({
						title: 'Kupon nyomtatás',
						width: 910,
						height: 520,
						minWidth: 910,
						minHeight: 470,
						layout: 'fit',
						plain: true,
						bodyStyle: 'padding:0px;',
						buttonAlign:'center',
						html: '<iframe SCROLLING=NO frameborder="0" style="width:910px; height: 470px;" width="920" height="470" id="couponframe" src="blankpage.php" onload="buildDom(\'' + src + '\');"></iframe>',
						modal: true,
						buttons: [{
							text: 'Nyomtatás',
							id: 'printframe'
						},{
							text: 'Kapcsolódó cikk',
							id: 'viewarticle'
						}]
					});
					window.show();
					Ext.get('printframe').on("click", function () {
						printIframe(src);
					});
					Ext.get('viewarticle').on("click", function () {
						mainwin.href = 'cikkek.php?cikk=' + resultobj.id;
					});
				} else {
					var window = new Ext.Window({
						title: 'Kupon nyomtatás',
						width: 910,
						height: 520,
						minWidth: 910,
						minHeight: 470,
						layout: 'fit',
						plain: true,
						bodyStyle: 'padding:0px;',
						buttonAlign:'center',
						html: '<iframe SCROLLING=NO frameborder="0" style="width:910px; height: 470px;" width="920" height="470" id="couponframe" src="blankpage.php" onload="buildDom(\'' + src + '\');"></iframe>',
						modal: true,
						buttons: [{
							text: 'Nyomtatás',
							id: 'printframe'
						}]
					});
					window.show();
					Ext.get('printframe').on("click", function () {
						printIframe(src);
					});
				}
			},
			success: function(result, request) {},
			failure: function(result, request) {
				// Sikertelen ellenorzes (nem jott letre a kommunikacio a szerverrel)
				Ext.MessageBox.alert('Hiba!', 'Sikertelen adatkommunikáció! A művelet sikertelen!');
				return false;
			}
		});
	}
	
	
	// pluggable renders
	function renderCoupon(value, p, record){
		if (value == '') {return '';}
		return '<a href="javascript:void(0);" onclick="clickOperation(\'' + value + '\');"><img src="' + value + '" alt="" width="165" height="165"></a>';
	}

    var grid = new Ext.grid.GridPanel({
		width:545,
		height:798,
		title:'Morex.hu kuponok',
		store: store,
		trackMouseOver:false,
		disableSelection:true,
		loadMask: true,
		
		// grid columns
		columns:[{
			id: 'picture', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
			header: "Kuponok",
			dataIndex: 'col0',
			renderer: renderCoupon,
			sortable: false
		}, {
			id: 'picture', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
			header: "Kuponok",
			dataIndex: 'col1',
			renderer: renderCoupon,
			sortable: false
		}, {
			id: 'picture', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
			header: "Kuponok",
			dataIndex: 'col2',
			renderer: renderCoupon,
			sortable: false
		}],
		
		
		// customize view config
		viewConfig: {
			forceFit:true,
			enableRowBody:true,
			showPreview:true,
			getRowClass : function(record, rowIndex, p, store){
				return 'x-grid3-row-collapsed';
			}
		},
		
		// paging bar on the bottom
		bbar: new Ext.Panel({
			html: '<p><a href="javascript:void(0);" onclick="showLoginPopup();">További kuponok</a></p>'
		})
	});
	
	// render it
	grid.render('home_kupons');
});

