$(document).ready(function(){
	tabsScript();
	
	// scroll
	$('div.scroll').jScrollPane({scrollbarWidth:'15'});
	
	// dynamic blocks
	$('span.open-close a').click(function(){
		if (!$(this).is('.openTab')) 
		{
			$('div.hide-content').hide(300);
			$('div.types').animate({height:'394px'},300);
			$(this).addClass('openTab').html('Read more').parents('div.inner-content').addClass('np').animate({height:'0px'},300,function(){
				$('div.control ul.tabset a:eq(0)').addClass('active');
				$('div.scroll').jScrollPane({scrollbarWidth:'15'});
				tabsScript();
			}) 
		} 
		else 
		{
			$('div.types').animate({height:'24px'},300);
			$('div.hide-content').show(300);
			$('div.control ul.tabset a').removeClass('active');
			$(this).removeClass('openTab').html('Close').parents('div.inner-content').removeClass('np').animate({height:'370px'},300, function(){
				$('div.scroll').jScrollPane({scrollbarWidth:'15'});
				tabsScript();
			});
		}
		return false;	
	});
	
	// tabs slide
	$('div.control').galleryScroll({
		btPrev:'a.prev',
		btNext:'a.next',
		disableClass: 'disable',
		scrollWindow: 900,
		scrollPadding: 6,
		circleSlide: false	
	});
	
	// cities slide
	$('div.top-cities').galleryScroll({
	    btPrev:'a.prev-arrow',
	    btNext:'a.next-arrow',
	    disableClass:'disable',
	    circleSlide: false,
	    scrollWindow: 610
	 });
	
	// vslide
//	$('#part').galleryScrollV({
//		btUp:'a.up',
//		btDown:'a.down',
//		holderList: 'div.list-hold',
//		scrollElParent: 'ul.list-1'
//	});
	
	//active inputs
//	$('.price input[type=text]').each(function(){
//		if ($(this).val() != '') $(this).parent().addClass('active');
//	});
//	$('.price input[type=text]').focus(function(){
//		$(this).parent().addClass('active');
//	});
//	$('.price input[type=text]').blur(function(){
//		if ($(this).val() == '') $(this).parent().removeClass('active');
//	});
	
	// tabs
	//tabsScript();
	
	// gallery
	$('div.G1').galleryLight({
		galleryList:'ul li a',
		largeIMG:'div.img-hold img',
		btNext:'a.next-photo',
		btPrev:'a.prev-photo'
	});
	$('div.G2').galleryLight({
		galleryList:'ul li a',
		largeIMG:'div.img-hold img',
		btNext:'a.next-photo',
		btPrev:'a.prev-photo'
	});
	
	// lightboxes
	$('a.enlarge-photo').click(function(){
		if ($.browser.msie && $.browser.version == '6.0') $('select').css({display:'none'});
		$('div.fader').css({display:'block'});
		$('div.gallery-popup').fadeIn(400);
		//poupUp_height('div.gallery-popup');
	});
	    $('a.close-window').click(function(){
		$('div.gallery-popup').fadeOut(400, function(){
		    $('div.fader').css({display:'none'});
		     if ($.browser.msie && $.browser.version == '6.0') $('select').css({display:'block'});
		});
	});
	$('a.enlarge-map').click(function(){
		if ($.browser.msie && $.browser.version == '6.0') $('select').css({display:'none'});
		$('div.fader').css({display:'block'});
		$('div.sub-map-popup').fadeIn(400);
		//poupUp_height('div.sub-map-popup');
	});
	$('.sub-map-popup a.close-window').click(function(){
	    $('div.sub-map-popup').fadeOut(400, function(){
		$('div.fader').css({display:'none'});
		 if ($.browser.msie && $.browser.version == '6.0') $('select').css({display:'block'});
	    });
	});
	
})

function poupUp_height(_popup)
{
	var _lightbox = $(_popup).get(0);
	var _popup_table = $('div.fader').get(0);
	var _minWidth = document.getElementsByTagName('div')[0].offsetWidth;
	
	var _height = 0;
	var _width = 0;
	if (window.innerHeight) {
		_height = window.innerHeight;
		_width = window.innerWidth;
	} else {
		_height = document.documentElement.clientHeight;
		_width = document.documentElement.clientWidth;
	}
	
	var _page = document.getElementsByTagName("body")[0];
	if (_lightbox && _popup_table && _page) {
		if (_height > _page.offsetHeight) _popup_table.style.height = _height + "px";
		else _popup_table.style.height = _page.offsetHeight + "px";
		if (_width < _minWidth) {_popup_table.style.width = _minWidth + "px";} else {_popup_table.style.width = '100%'}
		
		if (_height > _lightbox.offsetHeight) {
			if (!window.innerHeight) {
				_lightbox.style.position = "absolute";
				_lightbox.style.top = '0';
			} else {
				_lightbox.style.position = "absolute";
				_lightbox.style.top = '0';
			}
		}
		else {
			_lightbox.style.top = '0';
			_lightbox.style.position = "absolute";
		}
		if (_width > _lightbox.offsetWidth) _lightbox.style.left = (_width - _lightbox.offsetWidth) / 2 + "px";
		else {_lightbox.style.left = "0px";_lightbox.style.position = "absolute";}
	}
}

function tabsScript(){
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++) {
		if (sets[i].className.indexOf("tabset") != -1) {
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++) {
				if (links[j].className.indexOf("tab") != -1) {
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));
					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";
					
//					if (c.id.indexOf("tab") == -1)
//					{
//					    cleartotals();
//					}
					
					links[j].onclick = function () {
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1)); 
						if (c) {
							if ($(this).parents('div.control').length) {
								if ($('a.openTab').length || !$('span.open-close').get(0)) {
									//reset all tabs before change
									for (var i = 0; i < this.tabs.length; i++)
									{
										document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
										this.tabs[i].className = this.tabs[i].className.replace("active", "");
									}
									this.className += " active";
									c.style.display = "block";
									$('div.scroll').jScrollPane({scrollbarWidth:'15'});
									tabsScript();
								}
							} else {
								//reset all tabs before change
								for (var i = 0; i < this.tabs.length; i++)
								{
									document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
									this.tabs[i].className = this.tabs[i].className.replace("active", "");
								}
								this.className += " active";
								c.style.display = "block";
								$('div.scroll').jScrollPane({scrollbarWidth:'15'});
								tabsScript();
							}
							
						}
						return false;
					}
				}
			}
		}
	}
}


function formatDate(dateValue, format)
{
    var fmt = format.toUpperCase();
    var re = /^(M|MM|D|DD|YYYY)([\-\/]{1})(M|MM|D|DD|YYYY)(\2)(M|MM|D|DD|YYYY)$/;
    if (!re.test(fmt)) { fmt = "MM/DD/YYYY"; }
    if (fmt.indexOf("M") == -1) { fmt = "MM/DD/YYYY"; }
    if (fmt.indexOf("D") == -1) { fmt = "MM/DD/YYYY"; }
    if (fmt.indexOf("YYYY") == -1) { fmt = "MM/DD/YYYY"; } 
    var M = "" + (dateValue.getMonth()+1);
    var MM = "0" + M;
    MM = MM.substring(MM.length-2, MM.length);
    var D = "" + (dateValue.getDate());
    var DD = "0" + D;
    DD = DD.substring(DD.length-2, DD.length);
    var YYYY = "" + (dateValue.getFullYear()); 
    var sep = "/";
    if (fmt.indexOf("-") != -1) { sep = "-"; }
    var pieces = fmt.split(sep);
    var result = ""; 

    switch (pieces[0]) 
    {
        case "M" : result += M + sep; break;
        case "MM" : result += MM + sep; break;
        case "D" : result += D + sep; break;
        case "DD" : result += DD + sep; break;
        case "YYYY" : result += YYYY + sep; break;
    }

    switch (pieces[1]) 
    {
        case "M" : result += M + sep; break;
        case "MM" : result += MM + sep; break;
        case "D" : result += D + sep; break;
        case "DD" : result += DD + sep; break;
        case "YYYY" : result += YYYY + sep; break;
    }  

    switch (pieces[2]) 
    {
        case "M" : result += M; break;
        case "MM" : result += MM; break;
        case "D" : result += D; break;
        case "DD" : result += DD; break;
        case "YYYY" : result += YYYY; break;
    }

    return result; 
};