function getCookie(name) {
	var matches = document.cookie.match(new RegExp(
	  "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
	))
	return matches ? decodeURIComponent(matches[1]) : undefined 
}

function setCookie(name, value, props) {
	props = props || {}
	var exp = props.expires
	if (typeof exp == "number" && exp) {
		var d = new Date()
		d.setTime(d.getTime() + exp*1000)
		exp = props.expires = d
	}
	if(exp && exp.toUTCString) { props.expires = exp.toUTCString() }

	value = encodeURIComponent(value)
	var updatedCookie = name + "=" + value
	for(var propName in props){
		updatedCookie += "; " + propName
		var propValue = props[propName]
		if(propValue !== true){ updatedCookie += "=" + propValue }
	}
	document.cookie = updatedCookie
}

function deleteCookie(name) {
	setCookie(name, null, { expires: -1 })
}

var screen_curr = screen.width < 1200 ? 0 : 1;
if(getCookie('screen') === undefined || getCookie('screen') != screen_curr){
	setCookie('screen', screen_curr, {expires: 'Thu Jan 01 2030 03:00:00 GMT+0200'});
	window.location = window.location.href;
}

function changethumb(thumb){
	getSWF("containerHolder").initContainer(thumb);
}

function highlight(){
	jQuery('.form-field').each(function(n,element){
		jQuery(element).addClass('form-field-active');
		 setTimeout(function() { 
			jQuery(element).removeClass('form-field-active');
		 }, 2000);
	});
}

//deleteCookie('screen');
//alert(getCookie('screen'));

/*
*	Img preloader
*/

	var preloadFormImg  = new Image();
	preloadFormImg.src  = 'images/formfield_left_active.png';
	var preloadFormImg1 = new Image();
	preloadFormImg.src1 = 'images/formfield_active.png';

jQuery(function(){

	jQuery('div.tour-item').click(function(){
		jQuery('div.tour-item').each(function(n, el){
			jQuery(el).removeClass('tour-chk');
		});
		jQuery(this).addClass('tour-chk');
		jQuery(this).find('.tour-radio').attr('checked', true);
	});
	
	jQuery('input.tour-radio').each(function(n,element){ 
		if(jQuery(element).attr('checked') == true){
			jQuery(element).closest('div.tour-item').addClass('tour-chk')
		}
	});
	
	jQuery('input.payments-radio').each(function(n,element){ 
		if(jQuery(element).attr('checked') == true){
			jQuery(element).closest('div.payments').addClass('payments-chk')
		}
	});
	
	jQuery('div.payments').click(function(){
		jQuery('div.payments').each(function(n, el){
			jQuery(el).removeClass('payments-chk');
		});
		jQuery(this).addClass('payments-chk');
		jQuery(this).find('.payments-radio').attr('checked', true);
	});
	
		jQuery('input.payments-radio').each(function(n,element){ 
		if(jQuery(element).attr('checked') == true){
			jQuery(element).closest('div.payments').addClass('payments-chk')
		}
	});
	
	
	/*
	*	Image rotation
	*/
	var b = 0;
	
	function changeImg(){
		var funcArg = arguments.callee
		jQuery(funcArg.el).attr('src', funcArg.path + funcArg.arr[b]);
		tm = setTimeout(changeImg, 500);
		b++;
		b = b == funcArg.arr.length ? 0 : b;
	}
	
	jQuery('.thumb-small').click(function(){
		changethumb(curr_src);
		return false;
	});
	
	jQuery('.thumb-small').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
				curr_src = jQuery(this).attr('src');
				curr_path = curr_src.substring(0, curr_src.lastIndexOf('/') + 1);
				curr_id = jQuery(this).attr('id');
				curr_id = curr_id.substring(curr_id.lastIndexOf('-') + 1);
				changeImg.el = this;
				changeImg.path = curr_path;
				
				for(var i=0;i<thumb[curr_id].length;i++){
					var preloadImg = new Image();
					preloadImg.src = curr_path + thumb[curr_id][i];
				}
				
				changeImg.arr = thumb[curr_id];
				tm = setTimeout(changeImg, 500);
		} else {
				b = 0;
				clearTimeout(tm);
				jQuery(this).attr('src', curr_src);
		}
	});
	
	stop = false;
	allowRequest = 1;
	currPage = 2;
	
	function loadAddData(count,requestTour){
		$.ajax({
			url: '/index.php?tour='+ requestTour +'&page='+count,
			beforeSend: function(){
				allowRequest = 0;
				jQuery('#addItemsLoading').show();
			},
			complete: function(){
				allowRequest = 1;
				jQuery('#addItemsLoading').hide();
			},
			success: function(data) {
				$('#addItems').append(data);
			}
		});
	}

	if(window.requestTour !== undefined){
		$(window).scroll(function(){
			if($(window).scrollTop() == $(document).height() - $(window).height() && allowRequest == 1 && !stop){
				loadAddData(currPage,requestTour);
			}
		});
	}
	
});

