/* - - - - - - - - - - - - - - - - - - - - - - -
 * PLACES.TRAVEL
 * Javascript central control file
 - - - - - - - - - - - - - - - - - - - - - - - */

    $('html').addClass('js');

    $(function() {

        var ANVIL = {

            setup : function() {
                //$('a[rel=facebox]').facebox();
                //ANVIL.setup_ajax();
                //ANVIL.bookmark_setup();
                //ANVIL.tabs_setup();
                ANVIL.menu_setup();
                //ANVIL.search_setup();
                ANVIL.datepicker_setup();
                ANVIL.ad_setup();
                ANVIL.banner_setup();
                ANVIL.calendarpopup();
                ANVIL.profile_pics();
                ANVIL.highlight_pics();
                ANVIL.wysiwyg_setup();
                //ANVIL.mark_unmark();
                //ANVIL.news_scroller_setup();
            } ,

            /**
            * load tinymce for wysiwyg edit functionality
            */
            wysiwyg_setup : function () {
                // location of server. Unfortunately not dynamic, absolute URL necessary
                //var SERVER = 'http://localhost/stoerisme/anvil-stoerisme/application/views/default/';
                var SERVER = 'http://www.stellenboschtourism.co.za/application/views/default/';
                                                                    // alert(jQuery().tinymce);
                // Initializes all textareas that have class wysiwyg.
                if (jQuery().tinymce) {
                    $('textarea.wysiwyg').tinymce({
                        script_url : SERVER+'js/tiny_mce/tiny_mce.js' ,
                        mode : "exact" ,
                        theme : "advanced" ,
                        theme_advanced_toolbar_location : "top" ,
                        // for other buttons, see http://wiki.moxiecode.com/index.php/TinyMCE:Control_reference
                        //theme_advanced_buttons1 : "formatselect,removeformat,styleselect,bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,outdent,indent,|,bullist,numlist,|,charmap,|,undo,redo" ,
                        theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,|,link,unlink,anchor,image,bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,outdent,indent,|,bullist,numlist,|,charmap" ,
                        theme_advanced_buttons2 : "styleselect,formatselect,removeformat,fontsizeselect,|,undo,redo" ,
                        theme_advanced_buttons3 : "" ,

                        content_css : SERVER+"css/style.css" , // load styles defining look of the editor, also connected with styleselect button above
                        theme_advanced_styles : "Links=link,Lazy=lazy" , // only list custom styles
                        theme_advanced_blockformats : "h1,h2,h3,h4,h5,h6,blockquote,code"
                    });
                }
            } ,
            /*
             * Dropdown menus
             */
            menu_setup : function () {
                var ul = $("#col1 ul:not('.nav2')");
                //ul.hide(0);
                ul.each(function(i) {
                      el = $(this).prev();
                      el.click(function(j) {
                          $(this).next().slideToggle(350);

                          if ($(this).hasClass('navhead') ) {
                              $(this).toggleClass('navhead_down');
                              $(this).toggleClass('navhead_up');
                          }
                          return false;
                      });
                      if ($(this).hasClass('open'))
                         $(this).show(0);

                      if ($(this).hasClass('open') && $(this).hasClass('navhead') ) {
                         $(this).addClass('navhead_down');
                         $(this).removeClass('navhead_up');
                      }
                });
            } ,


            /* calendar popup */
            calendarpopup : function () {
                $('#calpop').hide(0);


                $("#calhov").hover(
                    function() { //over
                        $("#calpop").show(0).animate( { width:"200px",height:"215px" }, { queue:false, duration:600 } );
                    },
                    function() { //out
                        $('#calpop').fadeOut(400, function() {
                                              $('#calpop').css( 'width',"0px" )
                                              .css( 'height',"0px" );
                                              });
                    }
                );

                $('#calpop').hover(function(){$(this).show(0);});

            } ,


            /* profile pic select */
            profile_pics : function () {
                var pics = $('.jsselect');
                pics.each(function() {
                      $(this).click(function() {
                          var newsrc = $(this).attr('src');
                          newsrc = newsrc.replace(/\/small/i,"");
                          $('#bigpic').attr("src",newsrc);
                      });


                });

            } ,

            /* datepicker */
            datepicker_setup : function () {
                $('.datepicker').datepicker({
                    showOn: 'button',
                    buttonImage: 'anvil-stoerisme/application/images/calendar.gif',
                    buttonImageOnly: true
                    }
                );
                $.datepicker.setDefaults(
                    {
                    minDate: 0 ,
                    dateFormat: 'yy-mm-dd'
                    }
                );
                //$('.datepicker').datepicker('option', 'buttonImage', 'test.gif');
            } ,


        /* ad */
           ad_setup : function () {
               var ads1 = $('#adbox_top img');
               if (1 < ads1.length) {
                   var wait = $('#adbox_top img:not(:hidden)').parent().attr('rel');
                   setTimeout(function() { advert_cycle(ads1,0); }, wait*1000);
               }

               var ads2 = $('#adbox_sky img');
               if (1 < ads2.length) {
                   wait = $('#adbox_sky img:not(:hidden)').parent().attr('rel');
                   setTimeout(function() { advert_cycle(ads2,0); }, wait*1000);
               }
               var ads2 = $('#adbox_sky2 img');
               if (1 < ads2.length) {
                   wait = $('#adbox_sky img:not(:hidden)').parent().attr('rel');
                   setTimeout(function() { advert_cycle(ads2,0); }, wait*1000);
               }

               function advert_cycle(ads, idx, advert_timeout) {

                   $(ads.get(idx)).fadeOut(1000);
                   idx++;
                   if (!(idx < ads.length)) {
                       idx = 0;
                   }
                   setTimeout(function() { $(ads.get(idx)).fadeIn(1000); }, 500);
                   var time = $(ads.get(idx)).parent().attr('rel');
                   setTimeout(function() { advert_cycle(ads, idx); }, time*1000);
               }
         /*     function advert_cycle(type) {
                   if (type=='top')
                          $('#adbox_top').cycle();
                   if (type=='vert1')
                          $('#adbox_sky').cycle();

                   if (type=='vert2')
                          $('#adbox_sky2').cycle();

              }
         */
           },

           /* banner */
           banner_setup : function () {
               var ban = $('#bannerbox img');
               if (1 < ban.length) {
                   var wait = $('#bannerbox img:not(:hidden)').parent().attr('rel');
                   setTimeout(function() { banner_cycle(ban, 0); }, wait*1000);
               }
               function banner_cycle(banners, idx, banner_timeout) {
                    $('#bannerbox').cycle({timeout:5000});
               }
              /*  function banner_cycle(banners, idx, banner_timeout) {
                   $(banners.get(idx)).fadeOut(1000);
                   idx++;
                   if (!(idx < banners.length)) {
                       idx = 0;
                   }
                   setTimeout(function() { $(banners.get(idx)).fadeIn(1000); }, 500);
                   var time = $(banners.get(idx)).parent().attr('rel');
                   setTimeout(function() { banner_cycle(banners, idx); }, time*1000);
               }
               */

           },


           highlight_pics : function () {

               $("#experience").hover(
                    function() { //over
                        $(this).attr("src","application/views/default/images/experiencec.jpg");
                    },
                    function() { //out
                        $(this).attr("src","application/views/default/images/experiencebw.jpg");
                    }
                );
                $("#experience2").hover(
                    function() { //over
                        $("#experience").attr("src","application/views/default/images/experiencec.jpg");
                    },
                    function() { //out
                        $("#experience").attr("src","application/views/default/images/experiencebw.jpg");
                    }
                );

                $("#taste").hover(
                    function() { //over
                        $(this).attr("src","application/views/default/images/tastec.jpg");
                    },
                    function() { //out
                        $(this).attr("src","application/views/default/images/tastebw.jpg");
                    }
                );
                $("#taste2").hover(
                    function() { //over
                        $("#taste").attr("src","application/views/default/images/tastec.jpg");
                    },
                    function() { //out
                        $("#taste").attr("src","application/views/default/images/tastebw.jpg");
                    }
                );

                $("#discover").hover(
                    function() { //over
                        $(this).attr("src","application/views/default/images/historyc.jpg");
                    },
                    function() { //out
                        $(this).attr("src","application/views/default/images/historybw.jpg");
                    }
                );
                $("#discover2").hover(
                    function() { //over
                        $("#discover").attr("src","application/views/default/images/historyc.jpg");
                    },
                    function() { //out
                        $("#discover").attr("src","application/views/default/images/historybw.jpg");
                    }
                );

                $(".showme").hover(
                    function() { //over
                        $(this).attr("src","application/views/default/images/showmeD.jpg");
                    },
                    function() { //out
                        $(this).attr("src","application/views/default/images/showmeO.jpg");
                    }
                );

           }



        }

        ANVIL.setup();

    });



