function xtractFile(data){
	var m = data.match(/(.*)\/([^\/\\]+)(\.\w+)$/);
	if (m==null){
		return 0;
	}else{
		return {path: m[1], file: m[2], extension: m[3]}
	}
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent){
    	 curleft += obj.offsetLeft;
    }else if(obj.x){
        curleft += obj.x;
    }  
    return curleft;
  }

function c(m){
	console.log(m)
}
$(document).ready(function(){
	

	if (navigator.userAgent.indexOf('MSIE 7.0')==-1){
		$('div.fullfooter,div.fullpage,div.fulltop').ifixpng();
	};
	
	
	$("div.gallery_bigphoto img").livequery(function(){
		if (navigator.userAgent.indexOf('MSIE 7.0')==-1){
			$(this).ifixpng();
		};
	});

	$("img.scarf").livequery(function(){
		if (navigator.userAgent.indexOf('MSIE 7.0')==-1){
			$(this).ifixpng();
		};
	});



    $("div.offeritems a").livequery(function(){
        $(this).click(function(){
        	$('div.offeritems a.selected').removeClass('selected');
        	var prev = $(this).parent().prev().attr('id');
        	if (prev == undefined){
        		prev = 0;
        	}else{
        		prev = prev.replace('it','')
        	}
        	var next = $(this).parent().next().attr('id');
        	if (next == undefined){
        		next = 0;
        	}else{
        		next = next.replace('it','')
        	}
			var intab = $('div.container div.header ul li a.selected').attr('rel');
        	$(this).addClass('selected');

        	$parent = $(this).parent();
        	//alert(findPosX($parent[0]));
        	var width = parseInt($(this).parent().css('width').replace('px',''));
        	var currScroll = parseInt($('div.offeritemsbox').css('left').replace('px',''));
			//alert(parseInt($(this).parent().css('width').replace('px',''))-parseInt($('div.offeritemsbox').css('left').replace('px','')));
			//alert(findPosX($parent[0]) + currScroll);
			if ((findPosX($parent[0]) + (2*width) + currScroll) - 960 > 0){
				var delta = (findPosX($parent[0]) + (2*width) + currScroll) - 960; 
				$pane = $('div.offeritemsbox');
				$pane[0].scrollBy(width+delta+90,0);
				
			}else if ((findPosX($parent[0]) + currScroll - width) < 0){
				var delta = findPosX($parent[0]) + currScroll - width;
				$pane = $('div.offeritemsbox');
				$pane[0].scrollBy(-width+delta-45,0);
			};


			if ($(this).attr('rel').indexOf('grouts/') > 0){
				var toCut = $(this).attr('rel').indexOf('/c/');
				if (toCut == -1){
					var newRel = $(this).attr('rel');
					var c = $('div.groutscolors a.selected').attr('id');
					if (c != undefined){
						c = c.replace('c','');
						newRel = newRel+'/c/'+c;					
					}
				}else{
					var newRel = $(this).attr('rel').substring(0,toCut);
					var rest = $(this).attr('rel').substring(toCut+3);
	
					if (rest.indexOf('/') == -1){
						rest='';	
					}else{
						rest = rest.substring(rest.indexOf('/'));
					}
					var c = $('div.groutscolors a.selected').attr('id');
					c = c.replace('c','');
					newRel = newRel+'/c/'+c+rest;
				}
	            ajaxStart();
	            $ .get(newRel+'/prev/'+prev+'/next/'+next, function(){
	                ajaxEnd();
	            });

			}else if (intab == undefined){
	            ajaxStart();
	            $.get($(this).attr('rel')+'/prev/'+prev+'/next/'+next, function(){
	                ajaxEnd();
	            });
			}else{
	        	if (intab.indexOf('type/2') > 0){
		            ajaxStart();
		            $.get($(this).attr('rel').replace('/type/2','')+'/type/2/prev/'+prev+'/next/'+next, function(){
		                ajaxEnd();
		            });
									
				}else if (intab.indexOf('gallery') > 0){
		            ajaxStart();
		            $ .get($(this).attr('rel').replace('ashow','agallery')+'/prev/'+prev+'/next/'+next, function(){
		                ajaxEnd();
		            });
				}else{
		            ajaxStart();
		            $.get($(this).attr('rel').replace('/type/2','')+'/prev/'+prev+'/next/'+next, function(){
		                ajaxEnd();
		            });
					
				}				


			}
            return false;
        })
    });



    $("div.container div.offeritem a").livequery(function(){
        $(this).click(function(){
        	$('div.offeritems div#'+$(this).parent().attr('id').replace('fp','')+' a').click();
            return false;
        })
    });

    $("a.gallnav").livequery(function(){
        $(this).click(function(){
        	var toSel = $('div.offeritems a.selected').parent();
        	if (!($(this).hasClass('unactive'))&&(!($(this).hasClass('notgo')))){
				$(this).addClass('notgo');
				if ($(this).hasClass('gall_prev')||$(this).hasClass('item_prev')){
					toSel = toSel.prev().children('a');
					var oldRel = toSel.attr('rel');
					toSel.attr('rel',oldRel+'/lc/-1');
				}else{
					toSel = toSel.next().children('a');
					var oldRel = toSel.attr('rel');
					toSel.attr('rel',oldRel+'/lc/1');
				}
				/*
				$pane = $('div.offeritemsbox');
				if ($('div.offeritems').attr('id')=='hgallery'){
					var toScrollTo = toSel.parent().prev().prev().prev();
				}else{
					var toScrollTo = toSel.parent().prev().prev();
				}
				$pane[0].scrollTo('#'+toScrollTo.attr('id'));
				*/
				toSel.click();
				toSel.attr('rel',oldRel);
        	}
            return false;
        })
    });

	$("div.container div.header ul li a").livequery(function(){
         $(this).click(function(){
		 	if (($('div.offeritems').length >0)&&($('div.offeritems').attr('id') == 'bricks')) {
	            ajaxStart();
	            if ($(this).attr('rel')){
		            $.get($(this).attr('rel'), function(){
		                ajaxEnd();
		            });
		            return false;
	            }
			}	
        })
	});

	
	
	$('div.groutscolors a').livequery(function(){
         $(this).click(function(){
			var th = $(this);

			var background = th.attr("rel");
			$('#image-loading').show();
			var objImagePreloader = new Image();
			objImagePreloader.onload = function() {
				$('#bigimage').css('background','url("/'+background+'")');
				//	clear onLoad, IE behaves irratically with animated gifs otherwise
				objImagePreloader.onload=function(){};
				$('#image-loading').hide();
				$('div.gallery_bigphoto span#gcol').html(th.attr("title"));
				$('div.groutscolors a.selected').removeClass('selected');
				th.addClass('selected');
			};
			
			objImagePreloader.src = '/'+background;
			return false;
        })			
	});

	$('div.galleries a').livequery(function(){
		$(this).click(function(){
			var th = $(this);
			var background = th.attr("rel");
			$('#image-loading').show();
			$('#bigimage').fadeOut();
			$('div.gallery_bigphoto p span#glab').html('&nbsp;');
			var objImagePreloader = new Image();
			objImagePreloader.onload = function(){
				$('#bigimage').attr('src', '/' + background);
				$('div.gallery_bigphoto span#glab').html(th.attr("title"));
				$('#bigimage').fadeIn(function(){
					$('div.galleries a.selected').removeClass('selected');
					th.addClass('selected');
					$('#image-loading').hide();
				});
				
				//	clear onLoad, IE behaves irratically with animated gifs otherwise
				
				objImagePreloader.onload = function(){
				};
			};
			
			objImagePreloader.src = '/' + background;
			
			return false;
		});
	});


	$.each($("div#text a img").parent(),function(item,i){
		th = $(this);
		link = th.attr('href');
		var parsedObject = xtractFile(link);
		if (parsedObject!=0){
			var extension = parsedObject.extension;
			extension = extension.toLowerCase();
			if ((extension == '.jpg')||(extension == '.gif')||(extension == '.jpeg')||(extension == '.png')||(extension == '.bmp')){
				th.lightBox();			
			}
		}
	});

	$.each($("div.text a img").parents('a'),function(item,i){
		th = $(this);
		link = th.attr('href');
		if (link){
			var parsedObject = xtractFile(link);
			if (parsedObject!=0){
				var extension = parsedObject.extension;
				extension = extension.toLowerCase();
				if ((extension == '.jpg')||(extension == '.gif')||(extension == '.jpeg')||(extension == '.png')||(extension == '.bmp')){
					th.lightBox();			
				}
			}
			
		}
	});


	function ajaxEnd(){
	    $("#ajax_indicator").css('display', 'none');
	    $(".img_menu li a").addClass('active').removeClass('nactive')
	    $(".img_menu li a[href='#']").addClass('nactive').removeClass('active');
	}
	
	function ajaxStart(){
	    $("#ajax_indicator").css('display', 'block');
	}

	 $("table.tariff_table tr.tariffrow, table.techdata_table tr.tariffrow").hover(
	 	function(){
		 	$(this).addClass('hovered');
			
		},function(){
		 	$(this).removeClass('hovered');
		});
	
});