if (!ckm) var ckm = {};
ckm.Car = Class.create();
ckm.Car.prototype = {
    initialize: function() {
        this.currentPage = 0;
        this.IE6 = false;

		// hidden overflow ie6
		if (Prototype.Browser.IE && navigator.userAgent.indexOf("6", 0) > -1) {
            this.IE6 = true;
		}

    },
    init: function() {
//        History.address.setAddress('slide', this, "openSlide");
//        History.address.iecheckonlyhash = true;
//        History.address.init();
//        if (location.hash.indexOf("slide") > -1) {
//            this.openSlide(1);
//        }
        
//        if (GB_CURRENT) {
//            GB_CURRENT.addCallback(this.onClose);
//        }
        setInterval(ckm.car_.check.bind(ckm.car_), 100);
    },
    
    check: function() {
       if (location.hash.match(/photo=([0-9]+)/)) {
           if (this.currentPage == RegExp.$1) {
               return;
           }
           this.currentPage = RegExp.$1;

           if (!$("GB_overlay")) {
               this.openSlide(RegExp.$1);
           } else {
               this.switchSlide(RegExp.$1);
           }
//           location.hash = '#slide';
       }
    },
    
    openSlide: function(id) {
//       History.address.getVars();
//       var id = History.address.getParam('photo');
       if (id <1 || id >GB_SETS['carsphotos'].length) {
           return;
       }

       // ie6
       if (this.IE6) {
           $(document.body).setStyle({overflow: 'hidden'});
       }


       GB_myShowImageSet(GB_SETS['carsphotos'], id, this.onClose.bind(ckm.car_));



       // thumbnail
       new Insertion.After('GB_window', '<div id="GB_footer"></div>');
       
  	   var url = '/carsPhotos/index/' + ckm.car_serial;
	   var pars = '';

		
   	   var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars,
				onComplete: function(o){
                    $('GB_footer').innerHTML = o.responseText;
				    ckm.car_.setCurrent.call(ckm.car_, id);
//				    ckm.car_.setClose.call(ckm.car_);
				    }
			});
			
    },

    switchSlide: function(id) {
       if (id <1 || id >GB_SETS['carsphotos'].length) {
           return;
       }
//       var gb_sets = new GB_Sets({}, GB_SETS['carsphotos']);
       GB_CURRENT.switchPage(id)
       this.setCurrent(id);
//       alert(1);
       
    },
    
    setCurrent: function(id) {
       var photos = $$('div .gallery_thumbnail');
//alert(photos.length);
       for (var i=0; i<photos.length; i++) {
          Element.removeClassName(photos[i], 'current');
       }
       Element.addClassName("cars_photo_"+id, 'current');

    },
    
    setClose: function() {
       //se close button
        if (!$('GB_close_btn')) {
            var top = 30;
//           var top = parseInt(Element.getStyle('GB_window', 'top', 10)) -100;
//           if (top<0) {top=0;}
           var left = parseInt(Element.getStyle('GB_window', 'left', 10)) + 
             parseInt(Element.getStyle($$('.GB_frame')[0], 'width', 10)) + 50;
//             alert(parseInt(Element.getStyle($$('.GB_frame')[0], 'width', 10)))
           new Insertion.Before('GB_window', '<div id="GB_close_btn"><img src="/js/greybox/g_close.gif" /></div>');
           $('GB_close_btn').setStyle({'left': left+'px', 'top': top + 'px', position: 'fixed'} );
           Event.observe('GB_close_btn', 'click', GB_hide, false);
        }        
    },
    
    
    onClose: function() {
        this.currentPage = 0;
        Element.remove('GB_footer');
        if (navigator.userAgent.toLowerCase().indexOf('safari') > -1) {
            location.hash =  'list';
        } else {
            location.hash = "#list";
        }
           // ie6
           if (this.IE6) {
               $(document.body).setStyle({overflow: 'auto'});
           }
      }
}

// overridden
GB_Sets.prototype.switchPage = function (page) {
    if(page > 0 && page <= this.current_set.length+1) {
       this.current_index = page-1;
        this.updateFrame();
        this.updateStatus();
    }
}
GB_Sets.prototype.setWindowPosition = function() {
    var page_size = AJS.getWindowSize();
    var left = ((page_size.w - this.width)/2)-13;
    AJS.setLeft(this.g_window, left);


    if(!this.center_win) {
        AJS.setTop(this.g_window, AJS.getScrollTop());
    }
    else {
        var fl = ((page_size.h - this.height) /2) - 20 + AJS.getScrollTop();
        
        //up for thumbnail height
        fl -=  82/2;
        
        if(fl < 0)
            fl = 0;
        AJS.setTop(this.g_window, fl);
    }


   //se close button
    if (!$('GB_close_btn')) {
       new Insertion.Before('GB_window', '<div id="GB_close_btn"><img src="/js/greybox/g_close.gif" /></div>');
       Event.observe('GB_close_btn', 'click', GB_hide, false);
    }

       var top = fl - 50;
       if (top<0) {top=0;}
       var left = parseInt(Element.getStyle('GB_window', 'left', 10)) + 
         parseInt(Element.getStyle($$('.GB_frame')[0], 'width', 10)) + 50;
//             alert(parseInt(Element.getStyle($$('.GB_frame')[0], 'width', 10)))
       $('GB_close_btn').setStyle({'left': left+'px', 'top': top + 'px', 'z-index': 100} );


}

GB_myShowImageSet = function(set, start_index, callback_fn) {
    var options = {
        type: 'image',
        fullscreen: false,
        center_win: true,
        width: 600,
        height: 450,
        overlay_click_close: true
    }
    var gb_sets = new GB_Sets(options, set);
    gb_sets.addCallback(callback_fn);
    gb_sets.showSet(start_index-1);

    return false;
}


GB_hide = function() {
    GB_CURRENT.hide();
    $('GB_close_btn').remove();
}


//alert(GB_Gallery.prototype.toSource());

ckm.car_ = new ckm.Car;
Event.observe(window, 'load', ckm.car_.init.bindAsEventListener(ckm.car_), false);

