/**
 * @author nueysan.waldman
 * @author ferenc.radius
 */

$.scriptPath = "./assets/js/min/";
$.cssPath    = "./assets/css/";
$.requireCache = true;

$.require(["Class.js", "dom-extend.js", "Controller.js" ]);

/**
 * Catch all view (for all urls) will be replaced by correct model
 */
mvc.add(new mvc.Urls("/$", "/home$", "/index.php$", "/frontend.php$", "/frontend_dev.php$"),
    [
        "jcarousel.js",
        "swfobject.js",
        "carousel_voting.js"
    ], new mvc.Action(
    {
        success: function() {
            createCarouselVote();
        }
    })
);

/**
 * Catch all view (for all urls) will be replaced by correct model
 */
mvc.add(new mvc.Urls(""),
    [
        "smartSearch.js",
        "jquery.equalizecols.js",
        "popup.js"
    ], new mvc.Action(
    {
        success: function() {

/*            $('#search_container').smartSearch();
            $('#search_container_side').smartSearch();
            $('#search_container_homepage').smartSearch();
*/
            $('#homepage_standpunten_container .standpunt_wrapper').equalizeCols();
            $('#highlight_container .column').equalizeCols();
            $('#highlight_container .column2').equalizeCols();

            $("a[href*='url=']:not(.popup)").attr('target','_blank').attr('title','Deze link opent in een nieuw venster');
            $("a[href^='http://']:not(.popup)").attr('target','_blank').attr('title','Deze link opent in een nieuw venster');
            $("a.popup").popup(460, 640);
			$("<hr />").addClass("clearfix").insertBefore("#overview_container .three .column:nth-child(3n+1)");
			// $('#overview_container .three .column:nth-child(3n+1)').css('clear', 'left');
        }
    })
);

/**
 * article pages
 */
mvc.add(new mvc.Urls("standpunt/[0-9]+", "actueel/[0-9]+", "detail/[0-9]+"),
    [
//        "jquery.json.js",
//        "jquery.rating.js",
//        "printButton.js"

    ], new mvc.Action(
    {
        success: function() {


        }
    })
);


/**
 * styled select / forms
 */
mvc.add(new mvc.Urls("/zoeken", "search-form.html", "form-tests.html", "word-lid", "aanmelden", "registreren"),
    [
        "styledSelect.js"
    ], new mvc.Action(
    {
        success: function() {

            $("#search_filters select, #lid_form select, #registerform select:not(#childreninfo select)").styledSelect({

                afterOpenCallback: function() {
                    $(this).parent().css('z-index', '1000');
                },
                afterCloseCallback: function() {
                    $(this).parent().css('z-index', '0');
                }
            });

            function handleCountChildren(choice) {
                for(var i = 0; i < 5; i++) {
                    if(i <= choice && i > 0) {
                        $("#child_age_" + i).parent().show();
                        $("#child_name_" + i).parent().show();
                    } else {
                        $("#child_age_" + i).parent().hide();
                        $("#child_name_" + i).parent().hide();
                    }
                }
            }

            $("#childreninfo select").styledSelect({

                afterOpenCallback: function() {
                    $(this).parent().css('z-index', '1000');
                },
                afterCloseCallback: function() {
                    $(this).parent().css('z-index', '0');
                },
                afterChangeCallback: function(settings, container, select) {
                   handleCountChildren($(select).val());
                },
                initCallback: function(settings, container, select) {
                   handleCountChildren($(select).val());
                }
            });

            $("#member_birthdate_year, #registration_paper_entry_geboortedatum_year")
                .next("div.styledSelect").addClass("year");

            function parsePrices() {
                var totalPrice = 0.00;
                $("#priceinfo input[type='checkbox']:checked").each(function() {
                    var price = $(this).parent().find('.price').text().replace(',', '.').match(/[0-9.]+/)[0];
                    totalPrice += parseFloat( price );
                });
                $('#total_price span').text(totalPrice);
            }

            $("#priceinfo input[type='checkbox']").click(function() {
                parsePrices();
            });

            // handle on load
            parsePrices();

        }
    })
);

/**
 * scrolling standpunten
 */
mvc.add(new mvc.Urls("standpunten/overzicht", "standpunten.html"),
    [
        "scrollTo.js"
    ], new mvc.Action(
    {
       success: function() {
            $(".standpunten_index a, .link_up a").click(function(e) {
                e.preventDefault();
                $.scrollTo(this.hash, 500);
            });

       }
    })
);

mvc.add(new mvc.Urls("vvd-in-de-buurt", "maps.html"),
    [
        "LocateMap.js"
    ], new mvc.Action(
    {
       success: function() {

            /* gmaps form toggle*/
            $("#gmaps_options h4").css('cursor', 'pointer').click(function() {
                if($(this).parent().hasClass('collapsed')) {
                    $(this).parent().find("#gmaps_form").slideDown(125).end().removeClass('collapsed');
                } else {
                    $(this).parent().find("#gmaps_form").slideUp(125).end().addClass('collapsed');
                }
            });

           var map = new LocateMap("gmap", "NL", "./locator/locator.php", "#gmaps_search_results");
           map.init("Den Haag", ["#zipcode", "#city"]);
       }
    })
);

/**
 * Cufon font replacement needs to occur asap --Nuey
 */
mvc.add(new mvc.Urls(""),
    [
        "vvd.font.js"
    ], new mvc.Action(
    {
        onload: function() {
            Cufon.replace('h1, h2, h3, #highlight_container dt, #search_container_homepage legend');
            Cufon.replace('#navigation li.active ul a, #signup_component a', { hover: true });
        }
    })
);

