﻿var ImageBox = function(title, rootNodeId) {
	var positionAligned = false;
	var onload_flag = false;
	var imageBoxRoot = null;
	var imageBoxWrap = null;
	var imageBoxCloseNode = null;
	var imageBoxImageNode = null;
	var imageBoxImagePreviewNodes = [];
	var imageBoxImageNextNode = {};
	var imageBoxImagePrevNode = {};
	var imagesPreview = [];
	var images = [];
	var titles = [];
	var titleNode = null;
	var imagesSrc = [];
	var currectSelectedIndex = null;
	var imageBoxTopBgUrl = "/images/imagebox-bg-top.png";
	var imageBoxBottomBgUrl = "/images/imagebox-bg-bottom.png";
	var imageBoxBgUrl = "/images/imagebox-bg-middle.png";
	var scroller = null;
	
	function previewImageClick(e){
		this.imageBox.showImageBox();
		this.imageBox.showImage(this.imageIndex);
		var event = e || window.event;
		if (event.stopPropagation) {
			event.stopPropagation();
		} else {
			event.cancelBubble = true;
		} 
		return false;
	}
	function nextNodeClick(e){
		this.imageBox.showNextImage();
		return false;
	}
	function prevNodeClick(e){
		this.imageBox.showPrevImage();
		return false;
	}
	function closeButtonClick(e){
		positionAligned = false;
		this.imageBox.hideImageBox(e);
	}
	
	this.init = function(title, rootNodeId) {
		var rootNode = document
		if (rootNodeId != null){
			rootNode = document.getElementById(rootNodeId);
		}
		
		var ancors = rootNode.getElementsByTagName("a");
		for (var i = 0; i < ancors.length; i ++){
			if (ancors[i].getAttribute("rel") == "imagebox"){
				var imagePreview = new Image();
				if (ancors[i].getAttribute("preview") != null){
					imagePreview.src = ancors[i].getAttribute("preview");
				} else {
					imagePreview.src = ancors[i].getElementsByTagName("img")[0].src;
				}
				ancors[i].imageIndex = imagesPreview.length;
				ancors[i].imageBox = this;
				imagesPreview.push(imagePreview);
				titles.push(ancors[i].getAttribute("title") || "&nbsp;");
				imagesSrc.push(ancors[i].getAttribute("href"));
				ancors[i].onclick = previewImageClick;
			}
		}
		
		this.constructImageBox(title);
		
		for (var i = 0; i < imageBoxImagePreviewNodes.length; i ++){
			imageBoxImagePreviewNodes[i].imageIndex = i;
			imageBoxImagePreviewNodes[i].imageBox = this;
			imageBoxImagePreviewNodes[i].onclick = previewImageClick;
		}
		imageBoxImageNextNode.imageBox = this;
		imageBoxImageNextNode.onclick = nextNodeClick;
		imageBoxImagePrevNode.imageBox = this;
		imageBoxImagePrevNode.onclick = prevNodeClick;
	};
	this.constructImageBox = function(title){
		/*
		<div class="image-box-container-container image-box-container-top" id="test_box">
		<div class="image-box-container-bottom">
		<div class="image-box-container-content">
			<div class="image-box-content">
				<div class="image-box-header">
					<div class="button-close">×</div>
					Мультимедийный Плеер iPod classic 160GB Silver
				</div>
				<table class="image-box-preview-container"><tr>
					<td class="arrow"><span id="test_box_image_prev">←</span></td>
					<td><IMG alt="" border="0" src="http://uaprom-image.s3.amazonaws.com/311552_w40_h40_ipod_classis_silver11.jpg" id="test_box_image_preview_1"></td>
					<td><IMG alt="" border="0" src="./product_page_files/311553_w40_h40_ipod_classis_silver1.jpg" id="test_box_image_preview_2"></td>
					<td><IMG alt="" border="0" src="./product_page_files/311554_w40_h40_ipod_classis_silver2.jpg" id="test_box_image_preview_3"></td>
					<td><IMG alt="" border="0" src="./product_page_files/311555_w40_h40_ipod_classis_silver3.jpg" id="test_box_image_preview_4"></td>
					<td class="arrow"><span id="test_box_image_next">→</span></td>
				</tr></table>
				<div class="image-box-image">
					<img src="http://uaprom-image.s3.amazonaws.com/311552_w640_h640_ipod_classis_silver11.jpg" id="test_box_image"/>
				</div>
			</div>
		</div>
		</div>
		</div>
		*/
		
		imageBoxRoot = document.createElement("div");
		imageBoxRoot.className = "image-box-container  image-box-container-top";
		var imageBoxTopBg = new Image();
		imageBoxTopBg.src = imageBoxTopBgUrl;
		imageBoxRoot.style.backgroundImage = "url('" + imageBoxTopBgUrl + "')";
		
		var imageBoxBottom = document.createElement("div");
		imageBoxBottom.className = "image-box-container-bottom";
		var imageBoxBottomBg = new Image();
		imageBoxBottomBg.src = imageBoxBottomBgUrl;
		imageBoxBottom.style.backgroundImage = "url('" + imageBoxBottomBgUrl + "')";
		imageBoxRoot.appendChild(imageBoxBottom);
		
		var imageBoxMiddle = document.createElement("div");
		imageBoxMiddle.className = "image-box-container-middle";
		var imageBoxBg = new Image();
		imageBoxBg.src = imageBoxBgUrl;
		imageBoxMiddle.style.backgroundImage = "url('" + imageBoxBgUrl + "')";
		imageBoxBottom.appendChild(imageBoxMiddle);
		
		var imageBoxContent = document.createElement("div");
		imageBoxContent.className = "image-box-content";
		imageBoxMiddle.appendChild(imageBoxContent);
		
		//var titleNode = document.createElement("div");
		titleNode = document.createElement("div");
		titleNode.className = "image-box-header";
		imageBoxCloseNode = document.createElement("div");
		imageBoxCloseNode.className = "button-close";
		imageBoxCloseNode.appendChild(document.createTextNode('×'));
		imageBoxCloseNode.imageBox = this;
		imageBoxCloseNode.onclick = closeButtonClick;
		titleNode.appendChild(imageBoxCloseNode);
		titleNode.appendChild(document.createTextNode(title));
		
		
		var imagesPreviewRootNode = null;
		
		if (imagesPreview.length > 1) {
		  scroller = document.createElement("div");
		  scroller.className = "image-scroller";
		  
			imagesPreviewRootNode = document.createElement("div");
			imagesPreviewRootNode.className = "image-box-preview-container";
			
			
			imagesPreviewRow = document.createElement("div");
			imagesPreviewRow.className = 'image-container';

      scroller.appendChild(imagesPreviewRow);    
			
			var imagesPreviewPrev = document.createElement("div");
			imagesPreviewPrev.className = "arrow-prev";
			imageBoxImagePrevNode = document.createElement("span");
			imageBoxImagePrevNode.innerHTML = '←';
			imagesPreviewPrev.appendChild(imageBoxImagePrevNode);
			imagesPreviewRootNode.appendChild(imagesPreviewPrev);
			
			for (var i = 0; i < imagesPreview.length; i ++){
					var imagesPreviewTd = document.createElement("div");
					imageBoxImagePreviewNodes[i] = document.createElement("img");
					imagesPreviewTd.appendChild(imageBoxImagePreviewNodes[i]);
					imageBoxImagePreviewNodes[i].src = imagesPreview[i].src;
					imagesPreviewRow.appendChild(imagesPreviewTd);
			}
			
			var imagesPreviewNext = document.createElement("div");
				imagesPreviewNext.className = "arrow-next";
				imageBoxImageNextNode = document.createElement("span");
				imageBoxImageNextNode.innerHTML = '→';
				imagesPreviewNext.appendChild(imageBoxImageNextNode);
				imagesPreviewRootNode.appendChild(imagesPreviewNext);
				
			$$(imagesPreviewRow).width(imagesPreview.length*72);
			imagesPreviewRootNode.appendChild(scroller);
			}
		
		var imageContainerNode = document.createElement("div");
		imageContainerNode.className = "image-box-image";
		imageContainerNode.imageBox = this;
		imageContainerNode.onclick = closeButtonClick;
		imageBoxImageNode = document.createElement("img");
		imageContainerNode.appendChild(imageBoxImageNode);
		
		imageBoxContent.appendChild(titleNode);
   
    if (imagesPreviewRootNode){
      imageBoxContent.appendChild(imagesPreviewRootNode);
    }
		imageBoxContent.appendChild(imageContainerNode);
		
		imageBoxRoot.onclick = function(e){ var event = e || window.event;
										  if (event.stopPropagation) {
											event.stopPropagation();
										  } else {
											event.cancelBubble = true;
										  } 
								};
		imageBoxWrap = document.createElement('div');
		imageBoxWrap.className = 'image-box-wrap';
		imageBoxWrap.appendChild(imageBoxRoot);
		document.body.appendChild(imageBoxWrap);
				
		
		document.body.appendChild(imageBoxWrap);
	}
	
	this.showImageBox = function(){
		imageBoxWrap.style.display = "block";
    imageBoxWrap.style.top = this.alignPosition() + "px";  
		if (imagesSrc.length != images.length){
			for (var i = 0; i < imagesSrc.length; i ++){
				image = new Image();
				image.src = imagesSrc[i];
				images[i] = image;
			}
		}
		
		return false;
	}
	this.alignPosition = function(){
		//if (!positionAligned) {
			var windowHeight = 0;
			var scrollTop = 0;
			if (typeof(window.innerHeight) == 'number') {
				windowHeight = window.innerHeight;
				scrollTop = window.pageYOffset;
			}
			else {
				if (document.documentElement && document.documentElement.clientHeight) {
					windowHeight = document.documentElement.clientHeight;
					scrollTop = document.documentElement.scrollTop;
				}
				else {
					if (document.body && document.body.clientHeight) {
						windowHeight = document.body.clientHeight;
						scrollTop = document.body.scrollTop;
					}
				}
			}
			
			var boxTop = (windowHeight - imageBoxRoot.offsetHeight) / 2;
			if (boxTop < 0) {
				boxTop = 0;
			}
			
		positionAligned = true;
		return scrollTop+(windowHeight<600 ? 30 : (windowHeight/7)|0);
		//} 
	}
	this.hideImageBox = function(e){
		imageBoxWrap.style.display = "none";
	}
	this.showNextImage = function(index){
		var indexToShow = currectSelectedIndex;
		if (currectSelectedIndex == null) {
			indexToShow = 0;
		} else if (indexToShow == imagesPreview.length - 1){
			indexToShow = 0;
		} else {
			indexToShow = indexToShow + 1;
		}
		this.showImage(indexToShow);
	}
	this.showPrevImage = function(index){
		var indexToShow = currectSelectedIndex;
		if (currectSelectedIndex == null) {
			indexToShow = 0;
		} else if (indexToShow == 0){
			indexToShow = imagesPreview.length - 1;
		} else {
			indexToShow = indexToShow - 1;
		}
		this.showImage(indexToShow);
	}
	this.showImage = function(index){
		if (currectSelectedIndex != null && imageBoxImagePreviewNodes[currectSelectedIndex] != null) {
			imageBoxImagePreviewNodes[currectSelectedIndex].parentNode.style.borderBottom = "0";
		}
		if (imageBoxImagePreviewNodes[index] != null){
			imageBoxImagePreviewNodes[index].parentNode.style.borderBottom = "#1d89aa 3px solid";
		}
		imageBoxImageNode.src = images[index].src;
		images[index].imageBox = this;
		images[index].onload = function(){
			images[index].imageBox.alignPosition();
			onload_flag = true;
		};
		if(!onload_flag && navigator.userAgent.toLowerCase().indexOf("msie") != -1) images[index].imageBox.alignPosition();
		currectSelectedIndex = index;
		
		var scrollIndex = currectSelectedIndex;
		if (currectSelectedIndex <= 2) {
		   scrollIndex = 0;
		}
		else {
		   scrollIndex = currectSelectedIndex - 2;
		}
		  
		titleNode.innerHTML = '<div class="button-close" onclick="window.imagebox.hideImageBox();">×</div>'+titles[currectSelectedIndex];
    $$(scroller).animate({scrollLeft: scrollIndex*72}, 500);		
	}
	this.init(title, rootNodeId);
}


                jQuery(document).ready(function ($) {
                    
                  $.getJSON("http://tiu.ru/account/header/60?callback=?", function(data) {
                      $('.control-panel-wrapper').html(data['html']);
                  });
                  
            

              
      $('.pro-stars').hover(function(){
            $(this).css('cursor','help');
      });
              $('body').delegate('.pro-stars', 'click', function(){  
                  if ($$("#pro-companies-dialog").length > 0){
                        var dialog = $$("#pro-companies-dialog");
                  } else {
                      var dialog = $$('<div id="pro-companies-dialog"></div>')
                        .dialog({
                            autoOpen: false,
                            width: 600,
                            height: 480,
                            title: '<span class="pro-icon"></span> компании',
                            resizable: false,
                            position: ['center', 30],
                            dialogClass: 'dialog-customised'
                        });
                    $.getJSON(
                        "http://tiu.ru/util/ajax_get_pro_popup?callback=?", function(data){
                            $('#pro-companies-dialog').html(data['html']);
                        }
                    );
                  }
                  dialog.dialog('open');
      });

jQuery("#site_search_submit").button().click(function (event) {
  
});
jQuery("#button_contact").button({"icons": {"primary": null, "secondary": null}}).click(function (event) {
  
});
jQuery("#subscribe_submit").button().click(function (event) {
  
});

    window.imagebox = new ImageBox('\д\и\в\а\н\ \с\е\к\ц\и\о\н\н\ы\й');

jQuery("#contact-nostylew-button-16296").button({"icons": {"primary": null, "secondary": null}}).click(function (event) {
  
});

    var url = "http://tiu.ru/shopping_cart/update_elements_on_company_site?callback=?";
    data = {company_id: "60"};
    $$.getJSON(
        url, 
        data,
        function(data){
            // handling products state (is in cart or not) on company site //
            for(var i=0; i<=data.length; i++) {
                add_update_of_shopping_cart_elements(data[i]);
            }
    });


        $("#ajax_loading").data('ajax_stack', 0)
        .ajaxSend(function (event, xhr, options) {
            var $this = $(this);
			if (!SW.c["disable_ajax_indicator"]) { 
            	if (options.loading) {
                    $(options.loading).css("opacity", 0.7);
                    offset = $(options.loading).offset();
                    w = $(options.loading).outerWidth();
                    h = $(options.loading).outerHeight();
                    $this.css({position: "absolute", margin: 0, width: w, height: h, left: offset.left, top: offset.top, background: "none"});
                }
                $this.slideDown('fast');
                $this.data('ajax_stack', $this.data('ajax_stack') + 1);
            };
        })
        .ajaxComplete(function (event, xhr, options) {
            var $this = $(this);
			if (!SW.c["disable_ajax_indicator"]) { 
            	if(options.loading)
                	$(options.loading).css("opacity", 1);
                
                $this.data('ajax_stack', $this.data('ajax_stack') - 1);
                setTimeout(
                    function() {
                        if ($this.data('ajax_stack') <= 0) {
            	            $this.slideUp('fast');
                        };
                    },
                    100);
            };
        });
    

                var tzo = new Date().getTimezoneOffset();        
                document.cookie = "timezone_offset=" + escape(tzo * (-1));
            
                });
