/**
 * Init script
 */

$ (document).ready (
    function () {
        // set rel attribute
        /*
		var i = 0;
        while ($ ("a:eq(" + i + ") > img").length > 0) {
            //$ ("a:eq(" + i + ")").attr ("rel", "lightbox");
            //alert ($ ("a:eq(" + i + ")").attr ("rel"));
            alert(i);
            i++;
        }
        */
        
        // set calendar
        if ($ ("#input_date").length) {
            $('#input_date').datepicker();
        }
        
        // set news background
        if ($ (".div_medium_news").length) {
            var i = 0;
            while ($ (".div_medium_news:eq(" + i + ")").length > 0) {
                if (i % 2) {
                    $ (".div_medium_news:eq(" + i + ")").css ({
                        background: "#dfdbd9"
                    });
                }
                i++;
            }
        }
        
        // set google maps
        if ($ ("#div_google_maps").length) {
            $ ("#div_google_maps").css ({
                width: "710px",
                height: "710px",
                "text-align": "center",
                margin: "0 0 0 20px"
            });
            if (GBrowserIsCompatible()) {
                var map = new GMap2(document.getElementById("div_google_maps"));
                map.setCenter(new GLatLng(45.486643430649984, 12.416074275970459), 12);
                var point = new GLatLng(45.486643430649984, 12.416074275970459);
                map.addOverlay(new GMarker(point));
                map.addControl(new GSmallMapControl());
                map.addControl(new GMapTypeControl());
            }
        }
        
        // set lightbox popup
        if ($ ("a[rel=lightbox]").length) {
            $ ("a[rel=lightbox]").lightbox ();
        }
        
        // set default country
        if ($ ("#input_state").val ())
            $ ("#select_state").selectOptions ($ ("#input_state").val ());
        if ($ ("#input_state2").val ())
            $ ("#select_state2").selectOptions ($ ("#input_state2").val ());
        
        // set default date
        if ($ ("#select_date_day").length) {
            if ($ ("#input_date_day").val ())
                $ ("#select_date_day").selectOptions ($ ("#input_date_day").val ());
            if ($ ("#input_date_month").val ())
                $ ("#select_date_month").selectOptions ($ ("#input_date_month").val ());
            if ($ ("#input_date_year").val ())
                $ ("#select_date_year").selectOptions ($ ("#input_date_year").val ());
        }
        
        // set submit actions
        $ ("#inputs_signup").click (function () {
            $ ("#input_state").val ($ ("#select_state option:selected").val ());
        });
        $ ("#inputs_checkout").click (function () {
            $ ("#input_state").val ($ ("#select_state option:selected").val ());
            $ ("#input_state2").val ($ ("#select_state2 option:selected").val ());
            $ ("#inputc_facture").attr ("disabled", "");
            $ ("#inputc_gift").attr ("disabled", "");
        });
        
        // set macro categories flash
        if (!$ (".div_medium").length && !$ (".div_full").length && (
        $ ("#div_flash_table").length ||
        $ ("#div_flash_bath").length ||
        $ ("#div_flash_person").length ||
        $ ("#div_flash_accessories").length ||
        $ ("#div_flash_bedding").length)) {
            if ($ ("#div_flash_accessories").length) {st_name = "accessori"; id_str = "div_flash_accessories";}
            if ($ ("#div_flash_bath").length)        {st_name = "il_bagno";  id_str = "div_flash_bath";}
            if ($ ("#div_flash_bedding").length)     {st_name = "il_letto";  id_str = "div_flash_bedding";}
            if ($ ("#div_flash_person").length)      {st_name = "person";    id_str = "div_flash_person";}
            if ($ ("#div_flash_table").length)       {st_name = "la_tavola"; id_str = "div_flash_table";}
            
            swfobject.registerObject(id_str, "8.0.0", "expressInstall.swf");
            
            var flashvars = {};
            var params    = {menu: "false"};
            var attributes = {id: id_str};
            swfobject.embedSWF (
                "flash/" + st_name + ".swf",
                id_str,
                "750",
                "321",
                "8.0.0",
                "expressInstall.swf",
                flashvars,
                params,
                attributes
            );
            
            $ ("#div_content").css ({
                padding: "0",
                backgroundColor: "#fff"
            });
        }
        
        // set flash index
        if ($ ("#div_flash_index").length) {
            swfobject.registerObject("div_flash_index", "8.0.0", "expressInstall.swf");
            
            var flashvars = {};
            var params = {
                menu: "false"
            };
            var attributes = {
                id: "div_flash_index"
            };
            swfobject.embedSWF (
                "flash/home.swf",
                "div_flash_index",
                "750",
                "321",
                "8.0.0",
                "expressInstall.swf",
                flashvars,
                params,
                attributes
            );
        }
        
        // set vbars length
        $ ("img:last").load (function (){
            var page_height = $ ("#div_page").height ();
            $ ("#div_vbarsx").css ({
                height: page_height + "px"
            });
            $ ("#div_vbardx").css ({
                height: page_height + "px"
            });
        });
        var page_height = $ ("#div_page").height ();
        $ ("#div_vbarsx").css ({
            height: page_height + "px"
        });
        $ ("#div_vbardx").css ({
            height: page_height + "px"
        });
        
        // set flash zoom
        if ($ (".div_image img").length) {
            st_src = $ (".div_image img").attr ("src");
            st_src = st_src.substr (st_src.indexOf ("=") + 1); // no others parameters
            st_class = $ (".div_image").attr ("class");
            $ (".div_image").attr ("id", st_class);
            
            swfobject.registerObject("div_image", "8.0.0", "expressInstall.swf");
            
            var flashvars = {
                st_filename: st_src
            };
            var params = {
                menu: "false"
            };
            var attributes = {
                id: "div_image"
            };
            swfobject.embedSWF (
                "flash/zoom.swf",
                "div_image",
                "300",
                "300",
                "8.0.0",
                "expressInstall.swf",
                flashvars,
                params,
                attributes
            );
        }
        
        // set flash film
        $("#div_flash_film1").html('<object width="320" height="263"><param name="movie" value="http://www.youtube.com/v/6jOhE-YDgog&hl=en&fs=1&rel=0&border=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/6jOhE-YDgog&hl=en&fs=1&rel=0&border=1" type="application/x-shockwave-flash" allowfullscreen="true" width="320" height="263"></embed></object>');
        $("#div_flash_film2").html('<object width="320" height="263"><param name="movie" value="http://www.youtube.com/v/9iS61hRXLoQ&hl=en&fs=1&rel=0&border=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/9iS61hRXLoQ&hl=en&fs=1&rel=0&border=1" type="application/x-shockwave-flash" allowfullscreen="true" width="320" height="263"></embed></object>');
        /*
        if ($ ("#div_flash_film1").length) {
            swfobject.registerObject("div_flash_film1", "8.0.0", "expressInstall.swf");
            
            var flashvars = {
            };
            var params = {
                menu: "false",
                wmode: "transparent"
            };
            var attributes = {
                id: "div_flash_film1"
            };
            swfobject.embedSWF (
                "flash/film1.swf",
                "div_flash_film1",
                "340",
                "280",
                "8.0.0",
                "expressInstall.swf",
                flashvars,
                params,
                attributes
            );
        }
        
        if ($ ("#div_flash_film2").length) {
            swfobject.registerObject("div_flash_film2", "8.0.0", "expressInstall.swf");
            
            var flashvars = {
            };
            var params = {
                menu: "false",
                wmode: "transparent"
            };
            var attributes = {
                id: "div_flash_film2"
            };
            swfobject.embedSWF (
                "flash/film2.swf",
                "div_flash_film2",
                "340",
                "280",
                "8.0.0",
                "expressInstall.swf",
                flashvars,
                params,
                attributes
            );
        }
        */
        
        // correzione stile pulsante add to bag
        
        if($('html').attr('xml:lang') == 'en') {
            $('input.inputs_addproduct').attr('value','Add to bag');
            $('input.inputs_addproduct').css('paddingLeft',20);
            $('input.inputs_addproduct').css('width',150);
        }
        else {
            $('input.inputs_addproduct').attr('value','Aggiungi al carrello');
            $('input.inputs_addproduct').css('paddingLeft',60);
            $('input.inputs_addproduct').css('width',180);
        }
        
        // correzione allineamento prezzi tabella ecommerce
        for(var i = 0; i < $('table.table_ecommerce td').size(); i = i + 5) {
            $($('table.table_ecommerce td').get(i+2)).css('textAlign','right');
        }
        
        // set order
        order_switch ();
        facture_switch ();
        
        // set font
        //if (!jQuery.browser.msie) {
        /*
            var edwardian = {
                src: "fonts/cochin-8.swf"
            };
            
            sIFR.debugMode = true;
            sIFR.prefetch (edwardian);
            sIFR.activate ();
            sIFR.replace (edwardian, {
                selector: "h2",
                css: [
                    ".sIFR-root {position: absolute; left: 0; top: 0; text-align: left; font-weight: bold; width: 300px; background-color: #ebe8e6;}",
                    "a {color: #46392d; text-decoration: none; font-size: 40px;}",
                    "a:hover {color: #46392d;}"
                ]
            });
        */
        /*}
        else {
            $ ("h2 a"). css ({
                fontSize: "20px",
                color: "#342612",
                margin: "0 0 10px 0"
            });
            $ ("h2"). css ({
                margin: "0 0 10px 0"
            });
        }*/
    }
);

// enhanced ecommerce function which add to bag all selected products
function ecommerce_extra ()
{
    i = 0;
    st = "";
    bo_flag = false;
    while ($ (".table_ecommerce tr:eq(" + i + ")").length > 0) {
        st_color    = $ (".table_ecommerce tr:eq(" + i + ") td:eq(3) select option:selected"). val () ; // color
        in_quantity = $ (".table_ecommerce tr:eq(" + i + ") td:eq(4) select option:selected"). val () ; // quantity
        
        if (in_quantity && in_quantity != ".." && st_color && st_color != "..") {
            st += "{";
            st += "#" + $ (".table_ecommerce tr:eq(" + i + ") td:eq(0) input"). val (); // measure
            st += "#" + $ (".table_ecommerce tr:eq(" + i + ") td:eq(1) input"). val (); // form
            st_price = $ (".table_ecommerce tr:eq(" + i + ") td:eq(2) input"). val ();
            st_price = st_price.substring(st_price.indexOf (".", st_price) + 1);
            st += "#" + st_price; // price
            st += "#" + $ (".table_ecommerce tr:eq(" + i + ") td:eq(3) select option:selected"). val (); // color
            st += "#" + $ (".table_ecommerce tr:eq(" + i + ") td:eq(4) select option:selected"). val (); // quantity
            st += "}\r\n";
            bo_flag = true;
        }
        
        i++;
    }
    
    return (st)? st : false;
}

function order_switch ()
{
    if ($ ("#inputh_gift").length) {
        $ ("#inputc_gift").attr ("disabled", "disabled");
        
        if ($ ("#inputc_gift").attr ("checked")) {
            $ ("#div_gift").show ();
        }
        else {
            $ ("#div_gift").hide ();
        }
    }
    else {
        if ($ ("#inputc_gift").attr ("checked")) {
            $ ("#div_gift").show ();
        }
        else {
            $ ("#div_gift").hide ();
        }
        
        var page_height = $ ("#div_page").height ();
        $ ("#div_vbarsx").css ({
            height: page_height + "px"
        });
        $ ("#div_vbardx").css ({
            height: page_height + "px"
        });
    }
}

function facture_switch ()
{
    if ($ ("#inputh_facture").length) {
        $ ("#inputc_facture").attr ("disabled", "disabled");
        
        if ($ ("#inputc_facture").attr ("checked")) {
            $ ("#div_facture").show ();
        }
        else {
            $ ("#div_facture").hide ();
        }
    }
    else {
        if ($ ("#inputc_facture").attr ("checked")) {
            $ ("#div_facture").show ();
        }
        else {
            $ ("#div_facture").hide ();
        }
        
        var page_height = $ ("#div_page").height ();
        $ ("#div_vbarsx").css ({
            height: page_height + "px"
        });
        $ ("#div_vbardx").css ({
            height: page_height + "px"
        });
    }
}
