$(function() {
    initializeParentItem();
    bindGallery();
    bindCarousel();
    initializeRequirements();
    initializeOrder();
    initializeGoogleMaps();
    processMenusInIe();
});

initializeParentItem = function() {
    currentItem = ($('body').attr('class').split(' ',1))[0];
}


/* gallery */
bindGallery = function() {
    var gallery = $('#worksItems');
    if (gallery.size()) {
       gallery.each(function() {
           $(this).find('a').lightBox({
               txtImage: 'Изображение',
               txtOf: 'из'
           });
       });
    }    
}

/* carousel */
bindCarousel = function() {
    jQuery('#worksItems').jcarousel({
        auto: 5,
        scroll: 1,
        animation: 'normal',
        wrap: 'circular'
    });
};

/* requirements */
initializeRequirements = function() {
	var box = $('#requirements');
    if (box.size() > 0) {
		box.centerwindowhandler({
			bgzindex: 10000,
			bgclickable: false,
			fadespeed: 100,
			bgcolor: '#fff'
		});
        $('.requirementsIcon').click(function(){
            box.cwhStart();
            return false;
        });
        $('.requirementsCloseButton').click(function(){
            box.cwhStop();
            return false;
        });
	}
}

/* menus in ie */
processMenusInIe = function() {
    /* for ie 6 */
    if ($.browser.msie && parseInt($.browser.version)==6) {
        $('.leftMenu a, .subMenu a').mouseout(function(){
            if (!$(this).hasClass('active')) {
                $(this).find('span').css('background-image','none');
            }
        });
        $('.leftMenu a, .subMenu a').mouseover(function(){
            if (!$(this).hasClass('active')) {
                $(this).find('span').css('background-image','');
            }
        });
    }
}

/* order */
initializeOrder = function() {
    if (currentItem == 'outdoor'
            || currentItem == 'indoor'
                || currentItem == 'uv'
                    || currentItem == 'stickers') {
        $('.orderForm').validate({
            rules: {
                orderName: {
                    required: true
                },
                orderCompany: {
                    required: true
                },
                orderEmail: {
                    required: true,
                    email:true
                },
                orderPhone: {
                    required: true
                },
                orderEquipment: {
                    required: true
                },
                orderMaterial: {
                    required: true
                },
                orderSizeLength: {
                    required: true,
                    digits: true
                },
                orderSizeWidth: {
                    required: true,
                    digits: true
                },
                orderNumber: {
                    required: true,
                    digits: true
                },
                orderResolution: {
                    required: true
                }
            },
            messages: {
                orderName: {
                    required: '*'
                },
                orderCompany: {
                    required: '*'
                },
                orderEmail: {
                    required: '*',
                    email:'*'
                },
                orderPhone: {
                    required: '*'
                },
                orderEquipment: {
                    required: '*'
                },
                orderMaterial: {
                    required: '*'
                },
                orderSizeLength: {
                    required: '*',
                    digits: '*'
                },
                orderSizeWidth: {
                    required: '*',
                    digits: '*'
                },
                orderNumber: {
                    required: '*',
                    digits: '*'
                },
                orderResolution: {
                    required: '*'
                }
            },
            submitHandler: function(form) {
                $.post('/?reqMode=ajax&reqType=sendOrder', $('.orderForm').serializeArray(), function(response) {
                    if (response=='1') {
                        alert("Заказ отправлен");
                    } else {
                        alert("При отправке заказа произошла ошибка");
                    }
                } , 'html');
            }

        });
        $('.orderForm input:text').each(function() {
            if ($.trim($(this).val())!='') {
                var self= $(this);
                var val = self.val();
                self.focus(function() {
                    if ($.trim(self.val())==val) {
                        self.val('');
                    }
                });
                self.blur(function() {
                    if ($.trim(self.val())=='') {
                        self.val(val);
                    }
                });
            }
        });
        var box = $('#order');
        if (box.size() > 0) {
            box.centerwindowhandler({
                bgzindex: 10000,
                bgclickable: false,
                fadespeed: 100,
                bgcolor: '#fff'
            });
            $('.orderIcon').click(function(){
                box.cwhStart();
                return false;
            });
            $('.orderCloseButton').click(function(){
                box.cwhStop();
                return false;
            });
        }
        $('.orderForm input, .orderForm textarea').focus(function(){
            $('.orderSelectBig select, .orderSelectSmall select').hide();
        });
        $('.orderForm .orderRadio .orderRowBg, .orderForm .orderRadio label, .orderForm .orderCheckbox .orderRowBg, .orderForm .orderCheckbox label, .orderSelectBig .orderRowBg, .orderSelectSmall .orderRowBg').click(function(){
            $('.orderSelectBig select, .orderSelectSmall select').hide();
        });
        $('.orderForm .orderRadio .orderRowBg, .orderForm .orderRadio label').click(function() {
            var self=$(this).parent().find('div.orderRowBg');
            var parent = self.parent();
            var ident = $.trim(parent.attr('class').replace('orderRadio','').replace('orderRow',''));
            $('.orderForm .' + ident + ' .orderRowBg').removeClass('orderRadioActive');
            self.addClass('orderRadioActive');
            $('input#'+ident).val(parent.find('label').text().replace(':',''));
        });
        $('.orderForm .orderCheckbox .orderRowBg, .orderForm .orderCheckbox label').click(function() {
            var self=$(this).parent().find('div.orderRowBg');
            var parent = self.parent();
            var input = parent.find('input:hidden');
            self.toggleClass('orderCheckboxActive');
            if (self.hasClass('orderCheckboxActive')) {
                input.val(parent.find('label').text().replace(':',''));
            } else {
                input.val('');
            }
        });
        $('.orderSelectBig input, .orderSelectSmall input').focus(function() {
            var self=$(this);
            self.parent().find('select').show();
        });
        $('.orderSelectBig .orderRowBg, .orderSelectBig label, .orderSelectSmall .orderRowBg,  .orderSelectSmall label').click(function() {
            var self=$(this).parent().find('div.orderRowBg');
            self.parent().find('select').show();
        });

        $('.orderSelectBig select, .orderSelectSmall select').click(function(){
            var self=$(this);
            self.prev().val(self.find('option:selected').text());
            self.hide();
        });
        $('.orderSubmitButton').click(function() {
            $('.orderForm').submit();
            return false;
        });
    }
}

validateOrderForm = function() {
    return true;
}

/* maps */
initializeGoogleMaps = function() {
    $('#tulaMapWindow').centerwindowhandler({
        bgzindex: 10000,
        bgclickable: false,
        fadespeed: 100,
        bgcolor: '#fff',
        afterStart: function() {
            var mapName = 'tulaMap';
            var mapX = 54.198265;
            var mapY = 37.610064;
            if (GBrowserIsCompatible()) {
                createMap(mapName,mapX,mapY);
            }
        }
    });
    $('#moscowMapWindow').centerwindowhandler({
        bgzindex: 10000,
        bgclickable: false,
        fadespeed: 100,
        bgcolor: '#fff',
        afterStart: function() {
            var mapName = 'moscowMap';
            var mapX = 54.197571;
            var mapY = 37.618775;
            if (GBrowserIsCompatible()) {
                createMap(mapName,mapX,mapY);
            }
        }
    });
    $('#tula').click(function(){
        var mapName = 'tulaMap';
       // var mapX = 54.197571;
       // var mapY = 37.618775;
        if (GBrowserIsCompatible()) {
            //createMap(mapName,mapX,mapY);
            $('#' + mapName + 'Window').cwhStart();
        }
        return false;
    });
    $('#moscow').click(function(){
        var mapName = 'moscowMap';
        //var mapX = 54.197571;
        //var mapY = 37.618775;
        if (GBrowserIsCompatible()) {
           // createMap(mapName,mapX,mapY);
            $('#' + mapName + 'Window').cwhStart();
        }
        return false;
    });
    $('.mapCloseButton').click(function() {
        $(this).parent().cwhStop();
        return false;
    });
}

createMap = function(mapName,mapX,mapY) {
  if ($('#'+mapName).html()=='') {
      var map = new GMap2(document.getElementById(mapName));
      map.setCenter(new GLatLng(mapX, mapY), 15);
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      map.continuousZoomEnabled();
      map.enableScrollWheelZoom();      
      var point = new GLatLng(mapX,mapY);
      map.addOverlay(new GMarker(point));
  }
}



