//
//  FrontControler
//
//  Created by Thomas Goetz on 2007-06-05.
//

if(typeof Tati.core == "undefined"){
	Tati.core = {};
}

Tati.core.FrontController = function () {
	this.service = Tati.core.ServiceProxy.getService();
	EventBroadcaster.initialize(this);
	EventBroadcaster.initialize(this.service);
	this.service.addListener(this);
}

var o = Tati.core.FrontController;


// map

o.prototype.initmap = function(){

      if (GBrowserIsCompatible()) {
		this.mymap = new GMap2(document.getElementById("map_b"));        
		this.mymap.addControl(new GSmallMapControl());
		//this.mymap.addControl(new GMapTypeControl());
		this.mymap.setCenter(new GLatLng(48.25, 1.00), 6);
		this.mymap.enableDoubleClickZoom();
		GEvent.bind( this.mymap, "click", this, this.onMarkerClic );
      }

}

o.prototype.run = function(){

    $('body').append($('.jqmWindow'))
	$(".jqmWindow").jqm();
	
	if (typeof GMap2 != "undefined") {
	    this.initmap();
	    this.service.call( 'showShop', 'getShop', {search:SEARCH});
	}

}
o.prototype.showShopFromList = function ( id ) {
	this.currentId = id;
	this.service.call( "showShopInfo", 'getShopInfo', {shopId:id});	
}

o.prototype.onMarkerClic = function ( marker ) {
	var id = marker.id;

	if (id != undefined ) {
		this.currentId = id;
		this.service.call( "showShopInfo", 'getShopInfo', {shopId:id});
	}
}
o.prototype.showMapBlowup = function(){
	this.mymap.showMapBlowup(this.aMarker[this.currentId].getPoint());
}
o.prototype.showShopInfo = function(shop){
//	$("#shopInfo").html(shop);
	this.aMarker[this.currentId].openInfoWindowHtml( shop );
}


o.prototype.showShop = function(s){
	this.aMarker = Array();
	var shops = s[0];
	var position = s[1][0];

	for (var i in shops) {
		if (shops[i].glat != undefined && shops[i].glong!= undefined ) {
			var point = new GLatLng(shops[i].glat,shops[i].glong);
			
			if(accueil_tati==1){
				var pic = new GIcon();
			          pic.iconSize=new GSize(22,22);
			          pic.shadowSize=new GSize(22,22);
			          pic.iconAnchor=new GPoint(22,22);
			          pic.infoWindowAnchor=new GPoint(22,0);
				var pic = new GIcon(pic, "http://www.tati.fr/www/themes/tati/images/accueil/maquette-tati-map2_07.png");
				var marker = new GMarker(point,pic, {title:shops[i].name});
			}else{
				var marker = new GMarker(point, {title:shops[i].name});
			}
			
			marker.id = shops[i].shop_id;
			this.mymap.addOverlay(marker);	
			this.aMarker[ shops[i].shop_id ] = marker;
		}
		
	}
	
	if(active_search==1){
		this.mymap.setCenter(new GLatLng(48.8644, 2.343), 12);
	}else if ( position.maxlat != undefined ) {
		var boundsCenter = new GLatLngBounds(new GLatLng(position.maxlat,position.minlong),new GLatLng( position.minlat,position.maxlong));
		this.mymap.setCenter(boundsCenter.getCenter(), this.mymap.getBoundsZoomLevel(boundsCenter),G_NORMAL_MAP);		
	}

}
// admin 
o.prototype.addCategory = function(){
//	$("#newCat").show();
    $("#frmNewCategory").bind("submit",this.submit);
    this.afterForm = "reloadCategories";
    $("#newCat").jqmShow();
}

o.prototype.addPromo = function(){
    $("#homeAdmReceipt").html("");
      this.service.call( "newPromo", 'getNewPromo'); 

}
o.prototype.newPromo = function(h){

      $("#newPromo").html(h);
    $("#frmNewPromo").bind("submit",this.submit);
	this.afterForm = "loadPromos";
        
        $("#newPromo").jqmShow();
        var _self = this;        
        $("#newPromo #thumbnail").jqUploader({
	    background: "EDEDED",
	    barColor:   "FFFFFF",
	    width:  "320",
	    height: "35",
	    uploadScript: Tati.core.ServiceProxy._sUrl+"media/action/promo/",
	    allowedExt:     "*.jpg; *.jpeg; *.png; *.gif",
	    validFileMessage: 'Thanks, now hit Upload!',
	    hideSubmit: false,
	    callBack:_self.setPromoImg
	});
}

o.prototype.loadBlocks = function(){
        window.location.href = SGL_JS_CURRURL;
}

o.prototype.getEditBlock = function(id){
      $("#homeAdmReceipt").html("");
        this.nextId = id;
        this.service.call( "editBlock", 'getEditBlock', {blockId:id}); 
}

o.prototype.getEditPromo = function(id){
      $("#newPromo").html("");
        this.nextId = id;
        this.service.call( "editPromo", 'getEditPromo', {promoId:id}); 
}

o.prototype.editPromo = function(h){

      $("#newPromo").html(h);
      $("#frmNewPromo").bind("submit",this.submit);
      $("#promo_id_edit").val(this.nextId);
	this.afterForm = "loadPromos";
        
        $('body').append($('.jqmWindow'))        
        $("#newPromo").jqm().jqmShow();
        
        var _self = this;        
        $("#newPromo #thumbnail").jqUploader({
	    background: "EDEDED",
	    barColor:   "FFFFFF",
	    width:  "320",
	    height: "35",
	    uploadScript: Tati.core.ServiceProxy._sUrl+"media/action/promo/ID/"+this.nextId+"/",
	    allowedExt:     "*.jpg; *.jpeg; *.png; *.gif;",
	    validFileMessage: 'Thanks, now hit Upload!',
	    hideSubmit: false,
	    callBack:_self.setPromoImg
	});
}

o.prototype.editBlock = function(h){
      $("#homeAdmReceipt").html(h);
      $("#frmEditBlock").bind("submit",this.submit);
      $("#promo_id").val(this.nextId);
	this.afterForm = "loadBlocks";
        
        $('body').append($('.jqmWindow'))        
        $("#editBlock").jqm().jqmShow();
        
        var _self = this;        
        $("#editBlock #thumbnail").jqUploader({
	    background: "EDEDED",
	    barColor:   "FFFFFF",
	    width:  "320",
	    height: "35",
	    uploadScript: Tati.core.ServiceProxy._sUrl+"media/action/block/ID/"+this.nextId+"/",
	    allowedExt:     "*.jpg; *.jpeg; *.png; *.gif;",
	    validFileMessage: 'Thanks, now hit Upload!',
	    hideSubmit: false,
	    callBack:_self.setBlockImg
	});
}

o.prototype.submit = function(e){
	e.preventDefault();
	Tati.core.ServiceProxy.getService().formSubmit($(this));
}

o.prototype.onFormSubmited = function(){	
	$(".jqmWindow").jqmHide();	
	this[this.afterForm]();
}

o.prototype.reloadCategories = function(){
	this.service.call( "showCategories", 'getCategory');
}

o.prototype.showCategories = function(cat){
	$("#catrecept").html(cat);
}

o.prototype.delCategory = function(id){
	var conf = confirm("Voulez-vous vraiment supprimer cette categories ? Tous ses produits seront également supprimés !");
	if (conf) {
		this.service.call( "showCategories", 'delCategory',{catId:id});
	}
}

o.prototype.loadPromos = function(id){
        if (id != undefined ) {
                this.service.call( "updatePromo", 'getPromo', {promoId:id});
        } else {
                this.service.call( "updatePromo", 'getPromo');
        }                
}
o.prototype.reloadPromos = function(){
        this.service.call( "updatePromo", 'getPromo');
}
o.prototype.updatePromo= function(cat){
	$("#promorecept").html(cat);
    $('.gallery a').thickbox(
		{ width: '640', height: '600'}
	);
    pngFix();
}

o.prototype.setPromoImg= function(o,filename){
       Tati.core.ServiceProxy.getService().call( "showPromoImg", 'getUploadedImg', {filename:filename,type:'promo'});
}

o.prototype.setPromoImg1 = function(o,filename){
       Tati.core.ServiceProxy.getService().call( "showPromoImg", 'getPromoImg', {filename:filename,type:'promo'});
}

o.prototype.setBlockImg= function(o,filename){
        Tati.core.ServiceProxy.getService().call( "showBlockImg", 'getUploadedImg', {filename:filename,type:'block'});
}

o.prototype.showBlockImg= function(ret){
        if (ret != null ) {
                $("#imgRecept").remove();
                $("#editBlock #thumbnail").html("");
                $("#editBlock #thumbnail").html("<img src='"+SGL_JS_WEBROOT+"/images/Image/"+ret.file_name+"' /><input id='medId' type='hidden' name='block[media_id]' value='"+ret.media_id+"'>");
                $("#medId").val(ret.media_id);

        }
        
}

o.prototype.showPromoImg= function(ret){
        if (ret != null ) {
			$("#currentPromoImg").remove();
                $("#newPromo #thumbnail").html("<img src='"+SGL_JS_WEBROOT+"/images/Image/thumbs/small_"+ret.file_name+"' /><input type='hidden' name='promo[media_id]' value='"+ret.media_id+"'>");        
        }
        
}

o.prototype.uploadImage= function(type,callback){
	var _self = this;
	$("#thumbnail").jqUploader({
	    background: "EDEDED",
	    barColor:   "FFFFFF",
	    width:  "320",
	    height: "35",
	    uploadScript: Kindo.core.ServiceProxy._sUrl+"action/upload/type/"+type+"/",
	    allowedExt:     "*.jpg; *.jpeg; *.png; *.gif;",
	    validFileMessage: 'Thanks, now hit Upload!',
	    hideSubmit: false,
	    callBack:_self[callback]
	});
	
	$("#uploader").jqmShow();
}


o.prototype.delPromo = function(id) {
	var conf = confirm("Voulez-vous vraiment supprimer cette promo ?");
	if (conf) {
		this.service.call( "reloadPromos", 'delPromo',{promoId:id});
	}
}


// julien recrut

o.prototype.addRecrut = function() {
    $("#frmNewRecrut").bind("submit",this.submit);
	//this.afterForm = "reloadRecruts";
	$("#newRecrut").jqmShow();
}

o.prototype.showRecrutEditForm = function(recrutForm){
	$('#formEdit').html(recrutForm);
    
    $('body').append($('.jqmWindow'))
	$("#editRecrut_" +this.editFormId).jqm().jqmShow();
	$("#frmEditRecrut_" +this.editFormId).bind("submit",this.submit);
}

o.prototype.reloadRecrut = function(id) {
	window.location.href = SGL_JS_CURRURL;
}
o.prototype.editRecrut = function(id) {
	this.editFormId = id;
    this.service.call( "showRecrutEditForm", 'getRecrut',{recrutId:id});
	this.afterForm = "reloadRecrut";
}

o.prototype.delRecrut = function() {
	var conf = confirm("Voulez-vous vraiment supprimer cette offre d'emploi ?");
	if (conf) {
		return true;
	}
	return false;
}

delete(o);