
Cufon.replace('.cufon-cyr', { fontFamily: 'Romul', hover: true });
Cufon.replace('.cufon', { fontFamily: 'Trajan Pro', hover: true });


var s;
var obj;
var gal;
var fp;
var gmap;


$(document).ready(function(){

	
	// zoom

	if(typeof hs != "undefined") {

		hs.graphicsDir = BASE_URL + 'js/highslide/highslide/graphics/';
		hs.align = 'center';
		hs.transitions = ['expand', 'crossfade'];
		hs.outlineType = 'rounded-white';
		hs.fadeInOut = true;
		hs.showCredits = false;
		hs.preserveContent = true;

	}
	
	
    $('a.zoom').each(function() {
    	
		this.onclick = function() {
			
			return hs.expand(this);
	    };
    });
	

    $('a.map').each(function() {
    	
		this.onclick = function() {
	
			return hs.htmlExpand(this,  { objectType: 'iframe', width: 600, height: 500, objectLoadTime: 'after' } );
	    };
    });	
	    

	
    
    
	$('div#top-menu ul.top-menu li').hover(function(){
		
		$('div.sub', this).fadeIn('fast');
		
		$('a', this).first().addClass('act');
		
	}, function(){
		
		$('div.sub', this).stop(false, true);
		$('div.sub', this).fadeOut('fast');
		
		$('a:not(.active)', this).first().removeClass('act');
	})
	
	
	
	s = new Search();
	
	
	
	obj = new Objects();
	gal = new Gallery( $('div#image') );
	
	
	fp = new FP( $('div#fp') );
	
	
});









var Search = function() {
	
	var _self = this;
	var cena1;
	var cena2;
	
	
	this.doit = function(t) {
		
		var f = $('form#search-form');
		
		var link = $('select#ip option:selected').val();
		
		var vars = new Array('op', 'vieta', 'is1', 'is2', 'pl1', 'pl2', 'curr', 'cena1', 'cena2');
			
		if(t) {
			vars.push('type');
		}
		
		var gets = new Array;
		
		$.each(vars, function(i, it){
			
			var val = $('select#' + it).length != 0 ? $('select#' + it + ' option:selected').val() : $('input#' + it).val();

			
			if(val) {
				var str = it + '=' + val;
				gets.push(str);
			}
		})
				
		window.location.href = link + '?' + gets.join('&');
		
		return false;
	}
	
	
	
	
	this.op = function(el) {
		
		var op = $(el).val();
		
		var f 	= $('form#search-form');
		var ops	= $('select#op option', f);
		
		ops.attr('selected', '');
		
		ops.each(function(i, it){
			
			if( $(this).val() == op )
				$(this).attr('selected', 'selected');
		});
		
		_self.doit(true);
	}
	
	
	
	
	this.adjust = function() {
		
		var f 	= $('form#search-form');
		var op 	= $('select#op option:selected').val();
		
		if(op == 3) {
			
			$('div.cenas1', f).hide();
			$('div.cenas2', f).show();
			
			$('input#cena1', f).val( $('select#cenas2-cena1 option:selected').val() );
			$('input#cena2', f).val( $('select#cenas2-cena2 option:selected').val() );
			
		} else {
			
			$('div.cenas1', f).show();
			$('div.cenas2', f).hide();		
			
			$('input#cena1', f).val( $('select#cenas1-cena1 option:selected').val() );
			$('input#cena2', f).val( $('select#cenas1-cena2 option:selected').val() );		
			
		}
		
		var ip 	= $('select#ip option:selected').val();
		
		if( ip.match(/dziv/) ) {
			$('div.istabas', f).show();
		} else {
			$('div.istabas', f).hide();
			$('div.istabas select#is1 option', f).attr('selected', '');
			$('div.istabas select#is2 option', f).attr('selected', '');
		}
	}
	
	
	
	// init
	
	this.adjust();
	
	var f 	= $('form#search-form');
	$('select', f).change(function(){ _self.adjust(); })
	
	// darījuma tips objektu sarakstā
	$('div.sub-menu input').change(function(){ _self.op(this) })
	
}












var Gallery = function(el) {
	
	var _self = this;
	
	var sWidthMin 	= 10;
	var sWidthMax 	= 155;
	var data_index	= 0;
	var thumbWidth	= 105;
	
	
	// slider
	var perPage = 6;
	var currPage= 0;
	var offset 	= 105;	
	
	
	this.sw = function(n) {
		
		var items 	= $('div.image-large', el);
		var	curr 	= $(items.get(n));


		if( $('div.image-short', curr).length > 0 ) {
		
			_self.shortHide(function() { 
				
				items.not(curr).fadeOut(500);
				
				$('div.image-short-wrap', curr).hide();
				$(curr).fadeIn(500);
				
			});
		
		} else {

			items.not(curr).fadeOut(500);
			$(curr).fadeIn(500);
		}
	

		// pabīdās par vienu
	
		var slide = $('div.image-thumb-list div', el);
		
		var l = parseInt( slide.css('left') );

		
		if( l / offset + n == 0 && n > 0) { var left = l + offset; }		
		if( l / offset + n == 1 && n > 1) { var left = l + offset; }
		if( l / offset + n == 2 && n > 2) { var left = l + offset; }
		
		if( l / offset + n == 5 && items.length - 1 > n) { var left = l - offset; }
		if( l / offset + n == 4 && items.length - 2 > n) { var left = l - offset; }
		if( l / offset + n == 3 && items.length - 3 > n) { var left = l - offset; }
		
		
		if(typeof left != 'undefined')
			$(slide).animate({ 'left': left }, 750, 'easeOutExpo');
		
	}
	
	
	
	this.shortToggle = function(cb) {

		var width = $('div.image-short', el).width();

		if(width == sWidthMax) {
						
			_self.shortHide();
			
		} else {
		
			_self.shortShow();
			
		}
		
	}
	
	
	this.shortShow = function(cb) {
			
		//$('a.toggle', el).hide();
		
		$('div.image-short-wrap', el).stop(false, true);
		$('div.image-short-wrap').fadeIn();
		
		$('div.image-short', el).animate({ width: sWidthMax }, 500, function(){
			
			//$('a.show', el).hide();	
			//$('a.hide', el).show();	
			
			if(cb) { cb(); }
		});		
	}
	
	
	this.shortHide = function(cb) {
				
		//$('a.toggle', el).hide();
		
		$('div.image-short-wrap', el).stop(false, true);
		$('div.image-short-wrap').fadeOut();
		
		$('div.image-short', el).animate({ width: sWidthMin }, 500, function(){

			//$('a.hide', el).hide();
			//$('a.show', el).show();
			
			if(cb) { cb(); }
		});		
		
	}
	
	
	
	
	this.slide = function(p) {
		
		var slide = $('div.image-thumb-list div', el);
		
		var total = $('a.thumb', slide).length;
		var items = p * perPage;

		if(total < ((p + 1) * perPage)) {
		
			items = total - perPage;
			currPage = Math.ceil(items / perPage);
		}
		
		if(items < 0) {
			
			items = 0;
			currPage = 0;
		}
			
		var left = -(items * offset);	
		
		$(slide).animate({ 'left': left }, 750, 'easeOutExpo');
		
		
	}	
	
	
	
	
	
	this.next = function() {
		
		currPage = currPage + 1;
		
		_self.slide(currPage);
	}
	
	
	this.prev = function() {
		
		currPage = currPage - 1;
		
		_self.slide(currPage);
	}	
	
	
	
	this.goNext = function() {
		
		var slide = $('div.image-thumb-list div', el);
		
		var thumb = $('a.thumb.active', slide);		
		var thumbs 	= $('a.thumb', slide);
		
		var n = $('a', slide).index(thumb);
		n++;

		
		if(n >= (thumbs.length - 1) ) {
			n = 0;
		}
		
		
		var ne 	= $('a.thumb', slide).get(n);
		$(ne).click();

	}
	
	
	
	
	this.goPrev = function() {
		
		var slide = $('div.image-thumb-list div', el);
		
		var thumb = $('a.thumb.active', slide);		
		var thumbs 	= $('a.thumb', slide);
		
		var n = $('a', slide).index(thumb);
		n--;
		
		
		if(n < 0 ) {
			n = (thumbs.length - 1);
		}
		
		var pr 	= $('a.thumb', slide).get(n);
		$(pr).click();

	}	
	
	
	
	
	
	
	// init
		
	
	$('div.image-thumbs div.image-thumb-list a', el).click(function(){
		
		if( !$(this).attr('href') )
			return false;
		
		// highlight
		
		$('div.image-thumbs div.image-thumb-list a', el).removeClass('active');
		$(this).addClass('active');
		
		var i = $('div.image-thumbs div.image-thumb-list a', el).index(this);
		
		_self.sw(i);
		
		return false;
	})
	
	
	// toggle short
	
	$('a.toggle', el).click(function(){
		
		_self.shortToggle();
		
		return false;
	})
	
	
	$('div.images', el).hover(function(){
					
		_self.shortShow();
		
	}, function(){
		
		_self.shortHide();
		
	})
	
	
	// prev / next
	
	$('div.image-thumbs a.image-thumbs-prev').click(function(){
		
		_self.prev();
		
		return false;
	})
	
	$('div.image-thumbs a.image-thumbs-next').click(function(){
		
		_self.next();
		
		return false;
	})	
	
	
	// prev
	
	$('a.prev').click(function(){
		
		_self.goPrev();
		
		return false;
	})
	

	// next
	
	$('a.next').click(function(){
		
		_self.goNext();
		
		return false;
	})	
	
	
	// preload
	
	if(el) {	
		$('img.print', el).preload();
	}
	
}








var FP = function(el) {
	
	var _self = this;
	
	
	this.next = function() {
		
		var items 	= $('div.fps', el);
		var curr 	= items.index( $('div.fps:visible', el) );
		
		curr++;
		
		if(curr >= items.length) {
			curr = 0;
		}
		
		items.fadeOut('slow');
		$(items.get(curr)).fadeIn('slow');
		
		
		
		return false;
	}
	
	
	this.prev = function() {
		
		var items 	= $('div.fps', el);
		var curr 	= items.index( $('div.fps:visible', el) );
		
		curr--;
		
		if(curr < 0) {
			curr = items.length - 1;
		}
		
		items.fadeOut('slow');
		$(items.get(curr)).fadeIn('slow');
		
		
		return false;
	}	
	
	
	// init 
	
	_self.t = setInterval(_self.next, 4000);
	
	
	$('div.image', el).click(function(){
		
		var a = $('a.title', this);
		window.location.href = a.attr('href');
	});
	
	
	$(el).mouseover(function(){
		
		self.t = clearInterval(_self.t);
		
	}).mouseout(function(){
		
		_self.t = setInterval(_self.next, 4000);
	})
} 












var Objects = function() {
	
	var _self = this;

	
	
	this.curr = function(el) {
		
		var val = $(el).val();
		
		$('span', $('span.cena')).hide();
		$('span.' + val, $('span.cena')).show();
	}
	
	
	
	// init
	
	$('div.tabs ul.tabs li a').click(function(){
		
		var tabs = $('div.tabs ul.tabs li a');
		var i = tabs.index(this);
		
		var items = $('div.tabs div.tab'); 
		
		$(tabs).removeClass('active');
		$(tabs).removeClass('isNext');
		items.hide();
		
		$(this).addClass('active');
		$(items.get(i)).show();
		
		// bg fix
		
		if(tabs.get(i+1)) {
			
			$(tabs.get(i+1)).addClass('isNext');
		}
		
		return false;
	})
	

	
}






var KarteVietas = function(lat, lng, obj) {
	
	
	var _self = this;
	var _markers = new Array();
	
	
	
	this.clear = function() {
	
		$.each(_markers, function(){
			
			this.setMap(null);
		});
		
		
		var vietas = $('div#konfvietas-vietas');
		vietas.html('');
	}
	
	

	
	
	this.setMarkers = function(json) {
		
		_self.clear();
		
		var k = BASE_URL + 'images/ico/yellow.png';
		var d = BASE_URL + 'images/ico/red.png';
		
		$.each(json, function(n, item){

			var LatLng = new google.maps.LatLng(item.lat, item.lon);
		
		    var marker = new google.maps.Marker({
		        position: LatLng, 
		        map		: _self.map,
		        icon	: ((item.type == 'd') ? d : k),
		        title	: item.title
		    });			
			
		    var html = '<div class="marker">' +
							'<a href="' + item.link + '" class="image" style="background-image: url(\'' + item.image + '?mode=afit&w=50&h=50\');"></a>' +
		    				'<div class="title"><a href="' + item.link + '"><b>' + item.title + '</b></a></div>' +
							'<div class="address">' + item.address + '</div>' +
						'</div>';
    
		    var info = new google.maps.InfoWindow({
		        content: html 
		    });
		    
		    google.maps.event.addListener(marker, 'click', function() {
		    	  info.open(_self.map, marker);
		    });
		    
		    _markers.push(marker);
		    
		})
		
	}


	
	
	
	
	
	this.addMarkers = function(vietas) {


		this.setMarkers(vietas);
	}
	
	
	
	
	
	// init

    _self.myLatlng = new google.maps.LatLng(lat, lng);
    
    var myOptions = {
      zoom: 11,
      center: _self.myLatlng,
      mapTypeControl: false,
      navigationControl: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    
    _self.map = new google.maps.Map(document.getElementById(obj), myOptions);
    

    
}





function pdf(el) {
	
	var link = $(el).attr('href');
	
	var curr = $('select#viewCurr option:selected').val()
	
	console.log( curr );
	
	$(el).attr('href', link + '&curr=' + curr);

}







var addthis_config = {
	services_compact : 'twitter,facebook,draugiem,print,email,favorites'
}






