var noob = null;
var play_buttons = [];

var slide_move;
var slide_move_periodical;
function log(msg) {
return true;
    if (Browser.Engine.gecko) {
        console.log(msg);
    }
    else {
        alert(msg);
    }
}
window.addEvent('domready' , function(){
    if ($('deltypUpload')) {
        $('deltypUpload').addEvent('click', function(event) {
            $('upload_content').setStyle('display','block');
            $('email_content').setStyle('display','none');
			if ($('text_4_engraving')) 
				$$('#text_4_engraving textarea').setStyle('display','none');
				
				
			
        });
        $('deltypEmail').addEvent('click', function(event) {
            $('email_content').setStyle('display','block');
            $('upload_content').setStyle('display','none');
			if ($('text_4_engraving')) 
				$$('#text_4_engraving textarea').setStyle('display','block');
        });
    }
    //log("hello world");
   /* moved to elSelect
     if($$('.ONLY_QUANTITY'))
    {
        //log("hello world");
        $$('.ONLY_QUANTITY').addEvent('change', function(event) {
          // log(event+"hoo"); 
        });
    }*/
   if ($$(".value.wide.frames")) {
	$$(".value.wide.frames").addEvent('click', function(e) {
		$$(".pos_relative.overflow_auto").scrollTo(0,150);
	});
   }
    $$('.toggler').each(function(el){
    	var list_id = /[0-9]+$/.exec(el.id)[0].toInt();
    	
        var first = true;
        el.getElement('.top').getElements('a').each(function(link){
            if(first){
                link.getParent().addClass("current");
                first = false;
            }
            link.addEvent('click' , function(ev){
                new Event(ev).stop();
                link.getParent().getParent().getElements('a').each(function(sub_link){
                    sub_link.getParent().removeClass("current");
                });
                link.getParent().addClass("current");
                
                var catId = (/[0-9]+$/.exec(link.id))[0];
                el.getElements('.sidebox-body_reka').each(function(info_box){
                    if(info_box.id == "toggled_" + list_id + "_" +catId)
                        info_box.setStyle('display','block');
                    else
                        info_box.setStyle('display','none');
                })
            });
        });
        
        first = true;
        el.getElements('.sidebox-body_reka').each(function(info_box){
            if(first)
                first = false;
            else
                info_box.setStyle('display','none');
        });
    });
    
    if($('slidespot')){
        noob = new noobSlide({
            box: $('slidespot'),
            items: [0,1,2,3,4],
            interval: 200,
            startItem: 0,
            size:234
            
        });
        
       slide_move = function(index){
            if(index == null)
                index = (noob.currentIndex + 1) % play_buttons.length;
            noob.walk(index,false,false);
            play_buttons.removeClass('current');
            play_buttons[index].addClass('current');
            
            
        };
        
        
        play_buttons = $('play').getElements('span.walk');
        
        play_buttons.each(function(el, index){
            el.addEvent('click',function(){
                slide_move(index);
            });
        });
        var pause_button = $('pause');
        pause_button.addEvent('click',function(){
            var img = pause_button.getChildren()[0];
            var mode = /(_pu|_pl)/.exec(img.src)[0];
            var move_to = mode == "_pu" ? "_pl" : "_pu";
            if(mode == "_pu"){
                $clear(slide_move_periodical);
            } else {
                slide_move_periodical = slide_move.periodical(10000);
                
            }
            img.src = img.src.replace(mode , move_to);
            
        });
        slide_move(0);
        slide_move_periodical = slide_move.periodical(10000);
        //slide_move.periodical(5000);

    }
    preload_images = ['pink','yellow','green','lred','red','blue']
    f_images = []
    base_url="/skins/default_blue/customer/images/";
    preload_images.each(function (img){ f_images.push(base_url + img + ".gif"); f_images.push(base_url + img + "-hover.gif"); });
    var myImages = new Asset.images(f_images);

    /*$$('.images .fram a img').each(function(el){
    	
    	el.getParent().addEvent('click' , function(ev){
    		new Event(ev).stop();
    		
    		var split = /'(http(.*))&.*',(.*),(.*)/.exec(el.getParent().href)
    		var img_addr = split[1];
    		var img_width = split[3].replace('%20','').split('+')[0].toInt();
    		
    		var img_height = split[4].replace('%20','').split('+')[0].toInt();
    		
    		var product_id = null
    		$each(default_image,function (el , key){
    			product_id = key;
    			return false;
			});
			
    		//var mimg = 'det_img_' + product_id;
    		var mimg = $$('.main_image')[0];
    		mimg.src = img_addr;
    		mimg.addEvent('load' , function(){
    			mimg.width = img_width;
    			mimg.height = img_height;
			});
    		
		});
	});*/
	if($('zoom_box') && $('zoom_ind')){
		load_zoom();
	}
});
window.addEvent('load',function(){
	$('PreWrapper').addClass('load');
});
function show_frame(elm , orig_image){
	
	var frame_name = $(elm.id).className;
	//var find_value = $(elm.id).value;
	
	var mimg = $$('.main_image')[0];
	
	if(frame_name == ""){
		mimg.src = orig_image;
		
	} else {
		
		//mimg.src = ;
		//var io = new Image();
		mimg.src = "/frames/frame.php?image=" + escape(orig_image) + "&frame=" + escape(frame_name);
		//io.addEvent('load' , function(){
			//mimg.width = io.width;
			//mimg.height = io.height;
			//mimg.src = io.src;
    		/*mimg.width = mimg.naturalWidth;
    		mimg.height = mimg.naturalHeight;*/
		//});
		
	}
	
}
function zoom_image(src , width , height){
	var update_box = $('zoom_box').getElement('img').setProperties({
		'src':src,
		'width':width,
		'height':height
	});
  if ($('zoom_box')) {
    load_zoom();
  }
}


function load_zoom(){
		this.zoom_box = $('zoom_box').setStyle('overflow' , 'visible');
		this.img = zoom_box.getElement('img');
		//zoom_box
		//if(!$('inner_zoom'))
			this.inner_zoom = new Element('div', {'id':'inner_zoom'}).injectInside($('with-zoom')).setStyle('display' , 'none').setStyle('background-image',"url('" + this.img.src + "')");
            
		//else
		//	this.inner_zoom = $('inner_zoom').setStyle('background-image',"url('" + this.img.src + "')");
		this.cont_size = this.zoom_box.getSize();
		
		this.full_size = { x : this.img.width , y : this.img.height };
		
		this.ratio = Math.max(this.img.width / this.cont_size.x,this.img.height / this.cont_size.y);
		this.img.width = (this.img.width / this.ratio).toInt();
		this.img.height = (this.img.height / this.ratio).toInt();
        
        if ($('zoom_ind')) {
            $('zoom_ind').setStyle('width' , (400 / ratio).toInt() + "px")
                .setStyle('height' , (400 / ratio).toInt() + "px");
            }
		
		var zoom_cont_size = inner_zoom.getSize();
        
		
		
        
		/*img.addEvent('mousemove' , function(ev){
			//var position = { x : ev.client.x - img.getPosition().x , y : ev.client.y - img.getPosition().y }
			//console.log(ev);
			
			
			var position = {};
			//var real = {};
			
			if (Browser.Engine.gecko){
				position = { x : (ev.event.layerX * ratio).toInt()  , y : (ev.event.layerY * ratio).toInt() };
				//real = { x : ev.event.layerX.toInt()  , y : ev.event.layerY.toInt() };
			} else {
				position = { x : (ev.event.offsetX * ratio).toInt()  , y : (ev.event.offsetY * ratio).toInt() };
				//real = { x : ev.event.offsetX.toInt()  , y : ev.event.offsetY.toInt() };
			}
			//console.log(real);
			//console.log(position);
			start = { x : (position.x - 200) , y : (position.y - 200) }
			end = { x : (position.x + 200) , y : (position.y + 200) }
			if(start.x < 0){
				start.x = 0;
			} else if (end.x > full_size.x){
				start.x = full_size.x - 400;
			}
			
			if(start.y < 0){
				start.y = 0;
			} else if (end.y > full_size.y){
				start.y = full_size.y - 400;
			}
			//conosle.log(start);
			
			frame = {
				x : (start.x / ratio).toInt(),
				y : (start.y / ratio).toInt()
			}
            
			$('zoom_ind').setStyles({"top":frame.y + "px" , "left":frame.x + "px"});
			//console.log(frame);
			
			inner_zoom.setStyle('background-position', (full_size.x - start.x) + "px " + (full_size.y - start.y) + "px"); 
		})  */
        //First parameter of bind is the 'this'
        
        this.showZoomBox = function(){
            this.inner_zoom.setStyle('display' , 'block');
        }
        this.hideZoomBox = function(){
            this.inner_zoom.setStyle('display' , 'none');
        }
        
        this.zoom = function (ev){
            //var position = { x : ev.client.x - img.getPosition().x , y : ev.client.y - img.getPosition().y }
            //console.log(ev);
            //console.log(ev);
            //console.log(ev.page , img.getPosition());
            
            var position = {
                x:((ev.page.x - img.getPosition().x) * ratio).toInt(),
                y:((ev.page.y - img.getPosition().y) * ratio).toInt()
            };
            
            //var real = {};
            
            /*if (Browser.Engine.gecko){
                position = { x : (ev.event.layerX * ratio).toInt()  , y : (ev.event.layerY * ratio).toInt() };
                //real = { x : ev.event.layerX.toInt()  , y : ev.event.layerY.toInt() };
            } else {
                position = { x : (ev.event.offsetX * ratio).toInt()  , y : (ev.event.offsetY * ratio).toInt() };
                //real = { x : ev.event.offsetX.toInt()  , y : ev.event.offsetY.toInt() };
            }*/
            
            //console.log(real);
            //console.log(position);
            start = { x : (position.x - 200) , y : (position.y - 200) }
            end = { x : (position.x + 200) , y : (position.y + 200) }
            if(start.x < 0){
                start.x = 0;
            } else if (end.x > this.full_size.x){
                start.x = this.full_size.x - 400;
            }
            
            if(start.y < 0){
                start.y = 0;
            } else if (end.y > this.full_size.y){
                start.y = this.full_size.y - 400;
            }
            //conosle.log(start);
            
            var frame = {
                x : (start.x / this.ratio).toInt(),
                y : (start.y / this.ratio).toInt()
            }
            //console.log(start);
            $('zoom_ind').setStyles({"top":frame.y + "px" , "left":frame.x + "px"});
            //console.log(frame);
            
            this.inner_zoom.setStyle('background-position', (this.full_size.x - start.x) + "px " + (this.full_size.y - start.y) + "px"); 
        }
        $('zoom_ind').addEvent('mousemove' , this.zoom.bind(this));
        $('zoom_ind').addEvent('mouseenter' , this.showZoomBox.bind(this) );
        $('zoom_ind').addEvent('mouseout' , this.hideZoomBox.bind(this) );       
        $('zoom_ind').addEvent('mouseout', function() {
			$('zoom_ind').setStyle('display','none');
		});
        img.addEvent('mousemove' , this.zoom.bind(this));
        img.addEvent('mouseenter' , this.showZoomBox.bind(this) );
        img.addEvent('mouseout' , this.hideZoomBox.bind(this) );       
}

function tazuz_kzat()
{
     /*    if($('content_listmania_29_1001_29833') && $('content_listmania_15_1001_29833'))
            {
               $('content_listmania_15_1001_29833').setStyle('top','-220px');
               
               $$('.container').setStyle('height','900'+'px');
               var he = ($$('container').getStyle('height') - 220);    
               
            }                
     */}

