/**
 *  2Fresh
 * =========
 * page: Home
 *
 */

var HPYoutubePL = new Class({
  Extends: YoutubePL,
  options: {select:'',
            videoContainer:'demoContainer',
            videos:null},
  initialize: function(options){
      this.setOptions(options);
      if (this.options.select==""){
          this.buildSelect();
      }
  },
  loadPlayer: function(videoID){
    var so = new SWFObject("http://www.youtube.com/v/" + videoID + "&enablejsapi=1&playerapiid=player1",
                            "ytPlayer"+this.options.videoContainer,
                            "410", "250", "9", "#ffffff");
    so.addParam("allowScriptAccess", "always");
    so.write(this.options.videoContainer);

  }
});

var HPMiniGalleryYouTube = new Class({

  Extends: MiniGalleryYouTube,

  initialize: function (ele){
    this.element = $(ele);

    this.elements = {};

    this.YTVideos = [];

    if (this.element){

      this.elements.showroom = this.element.getChildren('.showroom')[0].getChildren('span')[0];
      this.elements.title = this.element.getChildren('.desc')[0];
      this.elements.buttons = this.element.getChildren('.buttons')[0].getChildren('li');

      // zjistime vsechna videa
      this.elements.buttons.each(function(item, index) {
          this.YTVideos[index] =item.getChildren('a')[0].get('rel');
      }, this);

      this.YTPL = new HPYoutubePL({
                              select : this.elements.buttons,
                              videoContainer : this.elements.showroom,
                              videos: this.YTVideos
                              });
      this.YTPL.run();

      this.attach();


    }

    return this;
  },
  itemSetActive : function (oid){
    if ($type(this.elements.buttons[this.lastActive]) == 'element'){
      this.elements.buttons[this.lastActive].removeClass('active');
    }

    activeElement = this.elements.buttons[oid];
    if ($type(activeElement) == 'element'){
      activeElement.addClass('active');
      activeElementA = activeElement.getChildren('a')[0];
      activeElementImg = activeElementA.getChildren('img')[0];

      this.lastActive = oid;

      this.showDetail(oid);

      //this.elements.title.set('text', activeElementImg.get('alt'));

    }
    return this;
  }
});

// akce co se provadi pokud je registrace starosty uspesna
RegistrationIsOk = function (){
    $('form-registraceStarosty').setStyle('display','none');
    $('box-isRegistrationOK').setStyle('display','block');
}

// ukladani
action_registrujStarostu = function (){
    var form_v = new FormValidator.Inline($('form-registraceStarosty'));

    if (form_v.validate()){
        new FF.JSON({
            'url' : baseUrl + 'a/mayorRegistration/create',
            'onTrue' : function(JSONobject){
                            RegistrationIsOk();
                            },
            'onFalse' : function(JSONobject){
                            Page.showErrorMessage('Chyba v registraci starosty, opakujte akci později');
                        }
        }).send($('form-registraceStarosty').toQueryString());
    }
}


// akce co se provadi pokud je nominace starosty uspesna
AdviceIsOk = function (){
    $('form-doporuceniStarosty').setStyle('display','none');
    $('box-isAdviceOK').setStyle('display','block');
}

// ukladani
action_doporucStarostu = function (){
    var form_v = new FormValidator.Inline($('form-doporuceniStarosty'));

    if (form_v.validate()){
        new FF.JSON({
            'url' : baseUrl + 'a/mayorRegistration/create',
            'onTrue' : function(JSONobject){
                            AdviceIsOk();
                            },
            'onFalse' : function(JSONobject){
                            Page.showErrorMessage('Chyba v nominaci starosty, opakujte akci později');
                        }
        }).send($('form-doporuceniStarosty').toQueryString());
    }
}



GMaps_ActivePoints = new Class({

	Implements: Options,

	options: {
            duration: 500,
            distance: 30,
            map : {
                    lat : 49.852152,
                    lon : 15.743408,
                    zoom : 7
            }
    },


    map : false, // main GMap object

    points: {}, // List of points for Map

	initialize: function(element, points, options){
		if (GBrowserIsCompatible()) {
            this.setOptions(options);

            this.element = document.id(element);

            this.points = this.parsePoints(points);

            this.build();
        }else{
            alert('no compatible browser with GMaps');
        }
	},

	build : function(){
        this.initGMaps();

        this.points.each(function(item, index) {
            item.marker = this.addMarker(item)
        }, this);

        this.map.setCenter(new GLatLng(this.options.map.lat, this.options.map.lon), this.options.map.zoom);
	},

    /**
     * Create GMaps Canvas
     */
    initGMaps : function (){
        // main GMap object
        this.map = new GMap2(this.element);
        // Set custom UI
        // TODO to options.gmap.config ?
        var customUI = this.map.getDefaultUI();
        customUI.maptypes.hybrid = false;
        customUI.maptypes.physical = false;
        this.map.setUI(customUI);
        this.map.enableContinuousZoom();
        //this.map.enableScrollWheelZoom();
    },

    parsePoints : function (points){
        return $H(points);

    },

    customOrder : function (marker) {
        return marker.newZIndex;
    },

    addMarker : function (point){
        myIcon = new GIcon();

        try {
            if (point.ico == 'red') {
                myIcon.image = "/data/pepsi/css/img/flag-red.png";
            }else {
                myIcon.image = "/data/pepsi/css/img/flag.png";
            }
        } catch (e) {}

        myIcon.shadow = '';
        myIcon.iconSize = new GSize(41, 54);

        myIcon.iconAnchor = new GPoint(0,54);
        myIcon.infoWindowAnchor = new GPoint(21,0);



        var marker = new GMarker(new GLatLng(point.position.lat, point.position.lon), {
                                        title: point.title,
                                        icon: myIcon,
                                        zIndexProcess: this.customOrder
                                    });



        marker.bindInfoWindowHtml('<span style="font-size:20px; font-family: arial; color: #214c7a; font-weight:bold">'+point.title+'</span>');


        try {

            if (point.ico == 'red') {
                marker.newZIndex = 10;
            }else{
                marker.newZIndex = 5;
            }

        } catch (e) {
            Log.write(e);
        }

        this.map.addOverlay(marker);
        return marker;
        //marker.openInfoWindowHtml('TEST');
    },

    goTo : function (name){
        point = this.points.get(name)

        //this.map.setCenter(point.marker.getLatLng(), 14);
        this.map.panTo(point.marker.getLatLng());
    }

});



window.addEvent('domready', function() {

    VideoPlayer = new HPMiniGalleryYouTube('box-videoGallery');

    //Dame dialogy "nahuru"
    //$$('body')[0].grab($('box-mayorRegistration'),'top');
    $$('body')[0].grab($('box-popupWindow_1'),'top');
    $$('body')[0].grab($('box-popupWindow_2'),'top');
    $$('body')[0].grab($('box-popupWindow_3'),'top');
    $$('body')[0].grab($('box-popupWindow_comments'),'top');


    // open close win 1
    $('btn-popupWindow_1').addEvent('click', function(e) {
        e.stop();
        $('box-popupWindow_1').removeClass('no');
    });
    $('btn_popupWindow_1_close').addEvent('click', function(e) {
        e.stop();
        $('box-popupWindow_1').addClass('no');
    });

    // open close win 2
    $('btn-popupWindow_2').addEvent('click', function(e) {
        e.stop();
        $('box-popupWindow_2').removeClass('no');
    });
    $('btn_popupWindow_2_close').addEvent('click', function(e) {
        e.stop();
        $('box-popupWindow_2').addClass('no');
    });

    // open close win 3
    $('btn-popupWindow_3').addEvent('click', function(e) {
        e.stop();
        $('box-popupWindow_3').removeClass('no');
    });
    $('btn_popupWindow_3_close').addEvent('click', function(e) {
        e.stop();
        $('box-popupWindow_3').addClass('no');
    });


    // open close win INFO !!!  --comments--
    $('btn-infoPopup').addEvent('click', function(e) {
        e.stop();
        $('box-videoGallery').addClass('no');
        $('box-popupWindow_comments').removeClass('no');
    });
    $('btn_popupWindow_comments_close').addEvent('click', function(e) {
        e.stop();
        $('box-videoGallery').removeClass('no');
        $('box-popupWindow_comments').addClass('no');
    });

/*
    // open close window registruj starostu
    $('btn-starosta').addEvent('click', function(e) {
        e.stop();
        $('box-mayorRegistration').removeClass('no');
    });
    $('btn_mayorRegistration_close').addEvent('click', function(e) {
        e.stop();
        $('box-mayorRegistration').addClass('no');
    });


    // open close window navrhni starostu
    $('btn-navrhni').addEvent('click', function(e) {
        e.stop();
        $('box-mayorAdvice').removeClass('no');
    });
    $('btn_mayorAdvice_close').addEvent('click', function(e) {
        e.stop();
        $('box-mayorAdvice').addClass('no');
    });
*/

    // Registrace Starosty
/*
    // Btn
    $('btn-registraceStarosty').addEvent('click', function(e) {
        e.stop();
        action_registrujStarostu();
    });

    // Form
    $('form-registraceStarosty').addEvent('submit', function(e) {
        e.stop();
        action_registrujStarostu();
    });
                          */

    // Navrzeni Starosty
/*
    // Btn
    $('btn-doporuceniStarosty').addEvent('click', function(e) {
        e.stop();
        action_doporucStarostu();
    });

    // Form
    $('form-doporuceniStarosty').addEvent('submit', function(e) {
        e.stop();
        action_doporucStarostu();
    });

                        */


/**
 * @@@ gMaps
 *===============================================================================
 */

    points = {

item_1: {
    title : "Unhošť, okr. Kladno",
    position : { lat: "50.090631", lon: "14.132195" }
},
item_2: {
    title : "Hrádek nad Nisou",
    position : { lat: "50.856243", lon: "14.844589" }
},
item_3: {
    title : "Písařov (okr. Šumperk)",
    position : { lat: "50.007298", lon: "16.801186" }
},
item_4: {
    title : "Svárov u Unhoště (Kladno)",
    position : { lat: "50.062649", lon: "14.150562" }
},
item_5: {
    title : "Městys Jince (poblíž Hořovic)",
    position : { lat: "49.785698", lon: "13.982849" }
},
item_6: {
    title : "Pňov-Předhradí (Kolín)",
    position : { lat: "50.086005", lon: "15.148773" }
},
item_7: {
    title : "Hajnice (Dvůr Králov)",
    position : { lat: "50.490498", lon: "15.920219" }
},
item_8: {
    title : "Hodonín",
    position : { lat: "48.851614", lon: "17.127686" }
},
item_9: {
    title : "Syrovice (Brno)",
    position : { lat: "49.078476", lon: "16.548500" }
},
item_10: {
    title : "Město Rotava (Karlovy Vary)",
    position : { lat: "50.296797", lon: "12.573681" }
},
item_11: {
    title : "Rudoltice (Č.Třebová)",
    position : { lat: "49.898615", lon: "16.569786" }
},
item_12: {
    title : "Výšovice (Prostějov)",
    position : { lat: "49.416669", lon: "17.139015" }
},
item_13: {
    title : "Řisuty",
    position : { lat: "50.216019", lon: "14.005852" }
},
item_14: {
    title : "Želechovice nad Dřevnicí (Olomouc)",
    position : { lat: "49.217821", lon: "17.745495" }
},
item_15: {
    title : "KLÍČANY (Od.Voda)",
    position : { lat: "50.201407", lon: "14.432430" }
},
item_16: {
    title : "Milevsko (Tábor)",
    position : { lat: "49.451164", lon: "14.362564" }
},
item_17: {
    title : "Králův Dvůr",
    position : { lat: "49.946581", lon: "14.036407" }
},
item_18: {
    title : "Milevsko",
    position : { lat: "49.451164", lon: "14.359818" }
},
item_19: {
    title : "Benecko",
    position : { lat: "50.666872", lon: "15.548744" }
},
item_20: {
    title : "Malovice (Plzeň)",
    position : { lat: "49.092305", lon: "14.223175" }
},
item_21: {
    title : "Jaroměr (Náchod)",
    position : { lat: "50.352471", lon: "15.913696" }
},
item_22: {
    title : "Brno, VUT, koleje a menzy",
    position : { lat: "49.235534", lon: "16.567383" }
},
item_23: {
    title : "Mukařov (Praha východ)",
    position : { lat: "49.993947", lon: "14.741421" }
},
item_24: {
    title : "Borovy (Plzeň)",
    position : { lat: "49.526657", lon: "13.302555" }
},
item_25: {
    title : "Stříbro (Plzeň)",
    position : { lat: "49.754211", lon: "13.000946" }
},
item_26: {
    title : "Nový Bor (Liberec)",
    position : { lat: "50.758396", lon: "14.554825" }
},
item_27: {
    title : "Žďár nad Sázavou",
    position : { lat: "49.562634", lon: "15.939789" }
},

item_29: {
    title : "Mladá Boleslav",
    position : { lat: "50.410924", lon: "14.903297" }
},
item_30: {
    title : "Desná",
    position : { lat: "50.758613", lon: "15.305328" }
},
item_33: {
    title : "Radostín nad Oslavou",
    position : { lat: "49.461876", lon: "15.965195" }
},
item_34: {
    title : "Moravské Knínice",
    position : { lat: "49.293673", lon: "16.501465" }
},
item_35: {
    title : "Protivín",
    position : { lat: "49.198756", lon: "14.217682" }
},
item_36: {
    title : "Čimelice",
    position : { lat: "49.465781", lon: "14.069710" }
},
item_37: {
    title : "Lázně Libverda",
    position : { lat: "50.890040", lon: "15.190659" }
},
item_38: {
    title : "Frymburk",
    position : { lat: "48.664437", lon: "14.168587" }
},
item_39: {
    title : "Kobeřice",
    position : { lat: "49.983462", lon: "18.043842" }
},
item_40: {
    title : "Žacléř",
    position : { lat: "50.653161", lon: "15.907173" }
},
item_41: {
    title : "Klášterec nad Ohří",
    position : { lat: "50.385538", lon: "13.172264" }
},
item_42: {
    title : "Havířov",
    position : { lat: "49.783924", lon: "18.433342" }
},
item_43: {
    title : "Hrubčice,Otonovice okres Prostějov",
    position : { lat: "49.438329", lon: "17.186050" }
},
item_44: {
    title : "Bystřice (okr. F-M)",
    position : { lat: "49.638733", lon: "18.721390" }
},
item_45: {
    title : "Karviná",
    position : { lat: "49.854808", lon: "18.540115" }
},
item_46: {
    title : "klenčí pod čerchovem",
    position : { lat: "49.437548", lon: "12.816238" }
},
item_47: {
    title : "Sušice",
    position : { lat: "49.231723", lon: "13.518677" }
},
item_48: {
    title : "Chrudim",
    position : { lat: "49.951220", lon: "15.795250" }
},
item_49: {
    title : "Český Těšín",
    position : { lat: "49.744672", lon: "18.624229" }
},
item_51: {
    title : "Planá nad Lužnicí",
    position : { lat: "49.354203", lon: "14.700737" }
},
item_52: {
    title : "Liberec ",
    position : { lat: "50.766865", lon: "15.056076" }
},
item_53: {
    title : "Brno ",
    position : { lat: "49.189333", lon: "16.611328" }
},
item_54: {
    title : "Ostrov",
    position : { lat: "49.929124", lon: "16.543007" }
},
item_55: {
    title : "Radčice",
    position : { lat: "50.796930", lon: "15.059938" }
},
item_56: {
    title : "Praha 13",
    position : { lat: "50.045345", lon: "14.322224" }
},
item_57: {
    title : "Děčín",
    position : { lat: "50.782497", lon: "14.214249" }
},
item_58: {
    title : "Zubří ",
    position : { lat: "49.570984", lon: "16.123638" }
},
item_59: {
    title : "Roudnice nad Labem ",
    position : { lat: "50.425581", lon: "14.261284" }
},
item_60: {
    title : "Praha 10 - Kolovraty",
    position : { lat: "50.011159", lon: "14.626923" }
},
item_61: {
    title : "Ostrovačice",
    position : { lat: "49.210196", lon: "16.410484" }
},
item_62: {
    title : "Praha 6",
    position : { lat: "50.100322", lon: "14.395523" }
},
item_63: {
    title : "Koleč",
    position : { lat: "50.198001", lon: "14.223175" }
},
item_64: {
    title : "Brno - Ořešín",
    position : { lat: "49.278277", lon: "16.606264" }
},
item_65: {
    title : "Bystřany",
    position : { lat: "50.628231", lon: "13.863888" }
},
item_66: {
    title : "Horské středisko v Krkonoších Strážné",
    position : { lat: "50.666872", lon: "15.606079" }
},
item_67: {
    title : "Hulín ",
    position : { lat: "49.317290", lon: "17.464485" }
},
item_68: {
    title : "Kladno ",
    position : { lat: "50.146986", lon: "14.104042" }
},
item_69: {
    title : "Česká Lípa",
    position : { lat: "50.686018", lon: "14.538345" }
},
item_70: {
    title : "Lišná",
    position : { lat: "49.885344", lon: "13.805008" }
},
item_71: {
    title : "Vraclav",
    position : { lat: "49.967896", lon: "16.091194" }
},
item_72: {
    title : "Višňová u Příbrami ",
    position : { lat: "49.705832", lon: "14.145584" }
},
item_73: {
    title : "Děčín",
    position : { lat: "50.781194", lon: "14.214249" }
},
item_74: {
    title : "Zlatá ",
    position : { lat: "50.040163", lon: "14.709063" }
},
item_75: {
    title : "Římov",
    position : { lat: "49.170032", lon: "15.757484" }
},
item_76: {
    title : "Šenov",
    position : { lat: "49.784811", lon: "18.375320" }
},
item_77: {
    title : "Praha 8",
    position : { lat: "50.132904", lon: "14.489937" }
},
item_78: {
    title : "Hrusovany nad Jevisovkou",
    position : { lat: "48.830374", lon: "16.401558" }
},
item_28: {
    title : "Ostrava - Nová Ves",
    position : { lat: "49.827464", lon: "18.226576" },
    ico : 'red'
},
item_31: {
    title : "Slaný ",
    position : { lat: "50.229199", lon: "14.086876" },
    ico : 'red'
},
item_32: {
    title : "Klatovy",
    position : { lat: "49.395781", lon: "13.294144" },
    ico : 'red'
},
item_50: {
    title : "Most",
    position : { lat: "50.504693", lon: "13.636780" },
    ico : 'red'
}
    }
    GMap_Points = new GMaps_ActivePoints("gmap", points );
/* /@@@  gMaps */


});

