function browserWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
  
}

CufonFontList = new Array(
	new Array('.cufon-GothamLight','GothamLight'),
	new Array('.cufon-GothamMedium','GothamMedium'),
	new Array('.cufon-GothamBook','GothamBook'),
	new Array('.cufon-GothamBold','GothamBold')
);
var openCount 				= 0;
var reservationFrameLoad 	= false;
function cufonReplace(){	
	
	for(i=0;i < CufonFontList.length;i++){
		cufonReReplace(CufonFontList[i][0], {fontFamily: CufonFontList[i][1], hover: true});
	}	
}


function openHeaderMask(val){
	
	
	openCount += val;
	//console.log('val :'+val,'openCount :'+openCount);

		if(openCount < 1){
			$('#headermask').stop(1,1).fadeOut();
			openCount = 0;
		}else{		
			$('#headermask').stop(1,1).fadeIn();
		};
	
};

function cufonReReplace(item,ops){
	if( navigator.userAgent.indexOf('MSIE') > 0 ){
		return false;	
	}
	Cufon.replace(item,ops);
}


// Plug-in's


////////////////////////////////////yeti tab menu

(function($) {
	$.fn.extend({

		tabmenu : function(options) {

			var defaults = {
				global		: '.tabs:not(.noJS)',
				container 	: '.heading',
				selector 	: 'ul li a',
				active		: 'active'
			};

			var options 	= $.extend(defaults, options);
			var selected 	= null;
			var selectedId 	= null;

			var o 			= options;
			var obj			= $(o.global);
			var clickObj	= obj.find(o.selector);
			var tabCont		= obj.find(o.container);
			
			var selectedTab = null;
			var selectedId 	= null;
			var setUrl 		= null;
			
			selectedTab = tabCont.find('ul li.active a');
			clickObj.click(function(){
				
				if(selectedTab != null){
					selectedTab.parent().removeClass('active');
					obj.find(selectedId).removeClass('active');
				}
				
				selectedTab = $(this);	
				selectedId  = $(this).attr('tabID');
				selectedTab.addClass(o.active);

				var splitHref = selectedTab.attr('href').split('index.html');
				
				setUrl = splitHref[splitHref.length - 1];		
				
				$(selectedTab).parent().addClass('active');
				$(selectedId).addClass('active');
				
				headingChanger();
				cufonReReplace($('.tabs .heading a.cufon-GothamLight'),{fontFamily: 'GothamLight', hover: true});
				locationChanger(splitHref[splitHref.length - 1]);
				
				return false;
			});
			
			function clickFirsItem(obj){	
				//console.log(obj);
				if(tabCont.find('ul li').hasClass('active')){
					selectedTab.click();
				}else{
					if(window.location.hash == ''){
						$(obj+':first').click();
					}else{
						//console.log($(obj).parent().parent());
						$(obj).parent().parent().find('a[href="'+window.location.href.replace('#','')+'"]').click();
					}
				}
			}
			
			function locationChanger(str){
				window.location.hash = '/'+str;
			}
			
			/*function headingChanger(){
				
				span = tabCont.find('span');
				if(span.html() != null){
					span.html(selectedTab.attr('total') + ' ' + selectedTab.text());
				}
			}*/
			
			function headingChanger(){
				
				span = tabCont.find('span');
				if(span.html() != null)
				{
					span.html(selectedTab.attr('total') + ' ' + span.text().replace(new RegExp('[0-9]','g'),''));
				}
			}
			
			clickFirsItem(o.global+' '+o.container+' '+o.selector);
		}
	});
})(jQuery);

//////////////////////////////////////yeti explorerMenuHoverPlugin
(function($) {
	$.fn.extend({
		explorerHover : function(options) {

			var defaults = {
				content		: '',
				imgContent	: '',
				selectItem	: ''
			};

			var options 		= $.extend(defaults, options);
			var selected 		= null;
			var selectedImg 	= null;

			return this.each(function() {
				var o 			= options;
				var obj			= $(this);
				var content		= $(o.content);
				var imgContent	= $(o.imgContent);
				var selectItem	= o.selectItem;
				var hoverObj	= content.find(selectItem);
				
				selected 		= content.find('.active');
				
				var splitId = selected.attr('id').split('-');
				var id = splitId[splitId.length-1];
				selectedImg = $(imgContent.find('#img-' + id));
				selectedImg.addClass('hover');

				hoverObj.hover(overHandler,outHandler);
								
				function overHandler(){
									
					if(selectedImg != null){
						selectedImg.removeClass('hover');
					}
					
					selected = $(this);	
					splitId = selected.attr('id').split('-');
					
					id = splitId[splitId.length-1];
					selectedImg = $(imgContent.find('#img-' + id));	
					selectedImg.addClass('hover');
					
					imgContent.find('.heading h1').text(selected.text());														
				}
				
				function outHandler(){			
					$(content.find('#a-' + id).addClass('active'));					
				}
						
				return false;			
			});
		}
	});
})(jQuery);
//////////////////////////////////////yeti timeline
(function($) {
	$.fn.extend({

		timeline : function(options) {

			var defaults = {
				content		: '.box-top',
				year		: '.year'
			};

			var options = $.extend(defaults, options);
			var selected = null;
			var selectedId = null;

			return this.each(function() {
				var o 			= options;
				var obj			= $(this);
				var clickObj	= obj.find('li');
				var content		= $(o.content);
				var year		= $(o.year);
				
				selected 		= obj.find('.active');
				selectedId 		= 'y-' + selected.text();

				clickObj.click(function(){
					
					if(selected){
						selected.removeClass('active');
						content.find('#'+selectedId).removeClass('active');
					}
					
					selected = $(this);
					selected.addClass('active');
					selectedId = 'y-' + selected.text();
					content.find('#'+selectedId).addClass('active');
					
					year.find('span').text(selected.text());
					
					cufonReReplace(obj.find('a'),{fontFamily: 'GothamBook', hover: true});
					cufonReReplace(year.find('span'),{fontFamily: 'GothamBook', hover: true});
					
					return false;
				});				
			});
		}
	});
})(jQuery);

///////////////////////////////////////////modalWin 
(function($) {
	$.fn.extend({

		modalWindow : function(options) {

			var defaults = {
				modal			: '',
				close			: '',
				maskTopPos		: 400,
				mask			: '',				
				maskType 		: 'normal',
				maskH			: '150',
				align			: 'center',
				callBack		: function(){},
				callBefore		: function(){},
				callClose		: function(){},
				callCloseBefore : function(){ return true; },
				ajax			: null,
				fixTop 			: false
				
			};

			var options = $.extend(defaults, options);
			var o 		= options; 
			var obj     = $(o.modal);
			var mask	= $(o.mask);
			var close	= obj.find(o.close);
			
			var objW	= obj.width();
			var objH	= obj.height();		
			
			var winW 	= $(window).width();
			var winH 	= $(window).height();									
			var docW 	= $(document).width();
			var docH 	= $(document).height();
			
		
			
			function openModal()
			{
				obj.stop(0,1).fadeIn('normal');
				if(o.align == 'center'){
					obj.css({
						'left': winW/2 - objW/2,
						'top' : (o.fixTop ? o.fixTop : (winH/2 - objH/2)) 	
					});
				}				
				
				close.click(function(){
					if(!o.callCloseBefore()){ console.log('Wait bro');return false; }
					obj.stop(0,1).fadeOut('normal');
					openHeaderMask(-1);
//					mask.stop(0,1).fadeOut('normal');
					$(window).unbind('scroll',popWindowEvent);
					
					if($('#ajaxContentPopup')){
						$('#ajaxContentPopup').remove();
					};			
					o.callClose();
					return false;
				});
				
				maskClickEvent = function(){
					close.click();
				};
				
				mask.click(maskClickEvent);
				
				mask.css({
					'display'	: 'block'
				});		
				
				popWindowEvent = function(){
					if($(window).scrollTop() < docH){
						$(window).resize();
					}
				};
				
				$(window).scroll(popWindowEvent);
				
				$(window).resize(function(){
					
					winW 	= $(window).width();
					winH 	= $(window).height();									
					docW 	= $(document).width();
//					docH 	= $(document).height();
					
					mask.css({
						width	: winW +'px',
						height	: docH +'px'
					});
					
					obj.stop(0,1).animate({
						'left': winW/2 - objW/2,
						'top' : (o.fixTop ? o.fixTop : (winH/2 - objH/2 +  $(window).scrollTop()+40))
					},300);
				});
				
				$(window).resize();
				
				o.callBack();
			}
			
			$(this).click(function(){
				
				openHeaderMask(1);
				
				o.callBefore();
				if(o.ajax != null)
				{
					$.ajax({
					  url		: o.ajax,
					  context	: document.body,
					  success	: function(data){
						
						$('body').append(data);
						obj     = $(o.modal);
						mask	= $(o.mask);
						close	=  obj.find(o.close);
						objW	= obj.width();
						objH	= obj.height();
						modalType = $(o.modalType);
						openModal();
					  }
					});
					
					return false;
				}
				openModal();
				return false;
			});
		}
	});
})(jQuery);

//////////////////////////////////////yeti booking

(function($) {
	$.fn.extend({

		booking : function(options) {

			var defaults = {
				selectBoxes			: '',
				selectBoxText		: '.div_selectbox_selected',
				selectBoxCont		: '.div_option',
				getque				: '.getquote',
				chechIn				: '.datepicker'
					
			};

			var options 				 = $.extend(defaults, options);
			
			var selectedBox 			 = null;
			var selectedItem 			 = null;
			var selectText 				 = null;
			var travelClickId 			 = $('#TRAVELCLICK_ID').val()?$('#TRAVELCLICK_ID').val():'17157';
			var alink 					 = "";

			return this.each(function() {
				var o 				= options;
				var obj				= $(this);
				var boxes 			= obj.find(o.selectBoxes);
				var clickObj		= boxes.find(o.selectBoxCont + ' ul li');				
				var chechInObj		= obj.find(o.chechIn);		
				var getque			= obj.find(o.getque);		
				
				selectText = $(boxes[0]).find(o.selectBoxText).text();			
				getque.click(function(){	
					switch (travelClickId) {
						case '72822':
							alink = "";
							break;
						case '17163':	
							alink = "";
							break;
						case '74155':	
							alink = "";
							break;
						case '17162':		
							alink = "";
							break;
						case '73859':			
							alink = "";
							break;
						case '17159':
							alink = "";
							break;
						case '75160':		
							alink = "";
							break;
						case '72850':		
							alink = "";
							break;
						case '17160':	
							alink = "";
							break;
						case '17157':	
							alink = "";
							break;
						case '73374':
							alink = "";
							break;
						case '17161':		
							alink = "";
							break;
						case '75760':		
							alink = "";
							break;
						default:
							break;
					}
					
					$('#trip-advisor').attr('href',alink);
					
					params = '';
					
					if($(chechInObj[0]).find('input').val())
					{
						params += 'dateIn='+$(chechInObj[0]).find('input').val()+'&dateOut='+$(chechInObj[1]).find('input').val();
					}
					
					params += '&lang='+$('#UI_LANG').val();
					
					if($('#REFER_ID').val())
					{
						params += '&ReferID='+$('#REFER_ID').val();
					}
					reservationFrameLoad = false;
					url = 'reservationd41d.html?'+(typeof($(this).attr('url')) == 'string' ? 'url='+escape($(this).attr('url')) : 'hotel_id='+ travelClickId + params);
					
					
					//For iPad new page open.
					if(navigator.userAgent.indexOf('iPad') > 0)
					{
						$(o.getque).each(function()
						{
							if($(this).get(0).tagName == 'A')
							{
								window.open(url);
								return false;
							}
							else
							{
								console.log('this is not anchor : '+$(this).get(0).tagName);
							}
						});
					}
					else
					{
						$('#online-rev').attr('src',url);
					}
					//$('#online-rev').attr('src','reservation.php?hotel_id='+ travelClickId + params);
				});
								
				clickObj.click(function(){

					if(selectedBox){
						selectedBox.removeClass('selectedbox');
					};				
										
					selectedBox = $(this).parent().parent().parent();
										
					if(selectedBox.parent().parent().index() == 2){
						travelClickId = $(this).attr('travelclick_id');
					};					
					
					if(selectedBox.parent().parent().index() == 0){
						boxes.each(function(i,v){
							if(i > 0){	
								$(v).find(o.selectBoxText).text(selectText);
							}
						});
					};
					
					selectedBox.addClass('selectedbox');
			
					selectedItem = $(this);
									
					boxes.each(function(i,v){
						
						if(i > 0){					
							
							if(! $(v).hasClass('selectedbox')){
								$(v).find(o.selectBoxCont + ' li').each(function(j,k){
									if(!$(k).hasClass(selectedItem.attr('key'))){
										$(this).hide();
									}
									else
									{
										$(this).show();
									}
								});
							};	
						};
					});		
				});	
			});
		}
	});
})(jQuery);


//////////////////////////////////////////////////////////////////////////////////////// navigation

(function($) {
	$.fn.extend({

		navigation : function(options) {

			var defaults = {
				slideButtons		: '',
				slideInfo			: '',
				slideDiv			: 'li',
				easing				: '',
				delay				: 800,
				itemHeight			: 162,
				itemWidth			: 178,
				itemPadding			: 25,
				viewItem			: 3
			};

			var options = $.extend(defaults, options);
			var selected = null;
			var selectedId = null;

			return this.each(function() {
				
				var o 				= options;
				var obj				= $(this);
				var objButtons 		= obj.find(o.slideButtons);
				var itemContainer	= obj.find(o.slideDiv);							
				
				var step = 0;
				var itemTotal = 0;
				var itemContW = 0;
				var isBound = false;
				var totalW = 0;	
				var viewItem = o.viewItem;
				
				itemTotal = itemContainer.find('li').length;
				itemContW = $(this).width();
				
								
				itemContainer.find('li').each(function(i,v){
					if($(this).hasClass('hover')){			
						if(i>=viewItem){				
							itemContainer.find('li').each(function(j,a){
								if(j < (itemTotal - viewItem)) {					
									totalW +=  $(a).width();
								}
							});
							
							itemContainer.css({
								left: - totalW
							});							
						}	
					}
				});
					
				objButtons.children('a').click(function(){							
					var val	= ($(this).attr('class'));
					
					switch (val) {
					case 'prev':
						menuSlider(-1,1);
						break;
					case 'next':
						menuSlider(1,-1);
						break;
					default:
						break;
					}
					return false;
				});
								
				function menuSlider(val,direc){
					isBound = false;
										
					var itemVal = val == 1? val : 0;
						
					step += val;
				
					if(step  > itemTotal - viewItem){
						step = itemTotal - viewItem;
						isBound = true;
					}
					
					if(step < 0  ){
						step = 0;
						isBound = true;
					}
					
					if(isBound) return;			
										
					totalW += direc * ($(itemContainer.find('li')[step-itemVal]).width() + o.itemPadding);
					
					itemContainer.stop().animate({
						left:(totalW)	
					},1000,'easeOutExpo');
					
					return false;		
				};
			
			});
		}
	});
})(jQuery);

(function($) {
	$.fn.extend({

		focuser : function(options) {

			var defaults = {
				input		: '.input:not(.datepicker) input,.textarea textarea'
			};

			var options = $.extend(defaults, options);

			var o 			= options;
			var input		= $(o.input);
			
			input.each(function(){
				$(this).attr('orjVal',($(this).text() ? $(this).text() : $(this).val()));
				
				$(this).focus(function(){
					if($(this).val() == $(this).attr('orjVal'))
					{
						$(this).val('');
					}
				});
				$(this).focusout(function(){
					if($(this).val() == '')
					{
						$(this).val($(this).attr('orjVal'));
					}
				});
			});
		}
	});
})(jQuery);

/**
 * Validation Engine Trigger
 * 
 * Belli class'a sahip bütün formlara validationEngine'i ekler.
 * Ajax submit destekler.
 * 
 * @author Uğur Erkan
 */
(function($) {
	$.fn.extend({

		validationEngineTrigger : function(options) {

			var defaults = {
				formClass				: '.formValidationEngine',
				elements 				: 'textarea,input',
				ajaxFormClass 			: 'ajaxSubmit',
				messageElementClassID 	: 'validationEngineAjaxMessageElement',
				ajaxLoader 				: '.ajaxLoader',
				securimageClass 		: '.securimageRefresh',
				securimageRefresh 		: true,
				addRandomID 			: true,
				resetForm 				: true,
				validationEngineOptions : {}
			};

			var options 	= $.extend(defaults, options);
			var o 			= options;
			

			if(!$().validationEngine){
				alert('Validation Engine Trigger Plug-in is needs validation engine dude.');
				return;
			}
			
			$(o.formClass).each(function()
			{
				if(o.addRandomID)
				{
					//Validation engine id olmadan çalışmıyor bu nedenle id veriyoruz idsiz her form elemanına
					$(this).find(o.elements).each(function(){
						
						if(!$(this).attr('id'))
						{
							$(this).attr('id','ajaxFormElementID_rnd'+Math.floor(Math.random()*9999999));
						}
						
					});
				}
				
				
				if($(this).hasClass(o.ajaxFormClass))
				{
					//Ajax durum mesajını formun ilk emenanında göstermek için ilk inputu bulup seçiyorum.
					$(this).find('input[type="text"]:first').addClass(o.messageElementClassID);
					
					$().extend(o.validationEngineOptions,
							{
								onValidationComplete : function(form, status)
								{
									if(status)
									{
										$.ajax({
											type 		: 'POST',
											url 		: $(form).attr('action'),
											data 		: $(form).serialize(),
											beforeSend 	: function()
														  {
															$(form).find(o.ajaxLoader).fadeIn();
														  },
										    success 	: function(status) 
										   				  {
										    				$(form).find(o.ajaxLoader).fadeOut();
										    				firstElementID = $(form).find('.'+o.messageElementClassID).attr('id');
														    status = status.split('[|split|]');
														    $('#'+firstElementID).validationEngine('showPrompt', status[0], status[1], true);
														    $('.'+firstElementID+'formError').delay(5000).fadeOut();
														    
														    if(o.resetForm && status[1] == 'pass')
														    {
														    	$(form).find(o.elements).each(function(){
														    		$(this).val($(this).attr('orjVal'));
														    	});
														    }
														    
														    if(o.securimageRefresh)
														    {
														    	$(form).find(o.securimageClass).trigger('click');
														    }
														    
														  }
										});
									}
								    
								}
							}
					);
				}
				
				$(this).validationEngine(o.validationEngineOptions);
		});
		}
	});
})(jQuery);

/**
 * Securimage Refresh Button
 * 
 * Sayfadaki tüm captchaların yenilenebilmesini ve biri yenilendiğinde 
 * diğerlerinden güncellenmesini sağlayan plugin
 * Class içim bkz: http://www.phpcaptcha.org/
 * 
 */
(function($) {
	$.fn.extend({

		securimageRefreshButton : function(options) {

			var defaults = {
				buttonClass				: '.securimageRefresh',
				imageClass 				: 'img.securimage',
				captchaURL 				: 'captcha',
				animButton 				: 'assets/securimage/images/refresh-anim.gif',
				normalButton 			: 'assets/securimage/images/refresh.gif'
			};

			var options 	= $.extend(defaults, options);
			var o 			= options;
			
			$(o.buttonClass).click(function(){
				if($(this).attr('disabled') == 'true'){ return false; }
				
				$(o.imageClass+':first').attr('src',o.captchaURL+'?'+Math.round(Math.random()*9999999995));

				obj = $(this);
				obj.css('background-image','url('+o.animButton+')');
				obj.attr('disabled','true');
				
				$(o.imageClass+':first').bind('load error',function(){
					image = $(o.imageClass+':first').attr('src');
					
					if($(o.imageClass).length > 1)
					{
						$(o.imageClass).each(function(){ $(this).attr('src',image); });
					}
						
					obj.css('background-image','url('+o.normalButton+')');
					obj.attr('disabled','false');
				});
				
				return false;
			});
		}
	});
})(jQuery);


//-----------------------------------------------------------------------------------------

$(document).ready(function(){
	
	
	
	
	cufonReplace();
	// Datepicker 
	if($().datepick)
	{
		$('.line-col .datepicker input').datepick({changeMonth: false,alignment:'bottomLeft'});
		
		$('.smallcol .input input').datepick({changeMonth: false, minDate: 0,alignment:'top',onSelect:function(target){
			$(target).css('top','850');
		}});
	}
	
		 
	$('.reservationbox-inner .div_selectbox').customSelectbox();
	
	$('#bookingarea .div_selectbox').customSelectbox({
		'direction':'up'
	});
	
	exploreTimer = setTimeout('void(0)',1);	
	$('#topmenu ul li.exploreRixos').mouseenter(explorerMenuOver).mouseleave(explorerMenuOut);	
	
	function explorerMenuOver()
	{
		
		$(this).addClass('hover');
		$(this).children('a').css({
			'color':'#fff'
		});
		
		cufonReReplace($(this).children('a'),{fontFamily: 'GothamMedium', hover: true});//	
		
		window.clearTimeout(exploreTimer);
		exploreTimer = setTimeout(setTime, 100);
		
		function setTime()
		{
			
			if( navigator.userAgent.indexOf('MSIE 7') > 0 ){
				$('.header-outer').css({
					'z-index':'5'
				});
			};	
			
			$('#exploreRixos').stop(1,1).slideDown(500,'easeInOut');
			openHeaderMask(1);
		}		
	};
	
	function explorerMenuOut()
	{
		if( navigator.userAgent.indexOf('MSIE 7') > 0 ){
			$('.header-outer').css({
				'z-index':'0'
			});
		};	
		
		//console.log($(this).parent());
		
		$(this).removeClass('hover');		
		
		$(this).children('a').css({
			'color':'#615249'
		});
				
		cufonReReplace($(this).children('a'),{fontFamily: 'GothamMedium', hover: true});
		
		window.clearTimeout(exploreTimer);
		exploreTimer = setTimeout(setTime, 100);

		function setTime(){
			$('#exploreRixos').stop(1,1).slideUp(400,'easeInOut');
			openHeaderMask(-1);
		}
	};
	
	$().cufonHover({selector : '#exploreRixos div.column',font : ''});
	
	//Footer
	if( navigator.userAgent.indexOf('iPad') > 0)
	{
		$('#bookingarea').css({
			'position'	: 'absolute',
			'bottom'	: '-'+($(window).height()+120)+'px'
		});
	}
	else
	{
		bookingAreaTimer = setTimeout(function(){},200);
		$(window).scroll(function(){ 
			window.clearTimeout(bookingAreaTimer);
			if($(window).scrollTop() > 400){
				bookingAreaTimer = setTimeout(function(){ $('#bookingarea').animate({'bottom':'0px'},500); },300);
			}else{
				bookingAreaTimer = setTimeout(function(){$('#bookingarea .div_selectbox .div_option').fadeOut(100); $('#bookingarea').animate({'bottom':'-100px'},500); },300);
			}
			
		});
	}
	
	
	//Search Box
	var toggleButton = true;
	$('#search a').click(function(){
		$('#searchbox').stop(0,1).slideToggle(500,'easeOutExpo');
		
		if(toggleButton){
			openHeaderMask(1);
		}else{
			openHeaderMask(-1);
		}
			
		toggleButton = !toggleButton;	
		
		return false;	
	});
	
	//Form Submit Anchor
	
	$('.formSubmitAnchor').click(function(){
		obj 	= $(this);
		evalStr = 'obj';
		for(i=0; i < $(this).attr('parent'); i++)
		{
			evalStr +='.parent()';
		}
		obj = eval(evalStr+';');
		obj.submit();
	});

	// Validation Trigger
	if($().validationEngine){ $().validationEngineTrigger(); }
	
	$().tabmenu();
	$().focuser();
	
	//Ajax content modal
	
	$('.contentPopUpTrigger').each(function()
		{
		id = $(this).attr('id','#contentTrigger_rnd'+Math.floor(Math.random()*99999));
		$(id).modalWindow({
			'ajax'		 : 'ajax/getContent?id='+($(this).attr('groupID') ? $(this).attr('groupID')+'&fromGroupID=true' : $(this).attr('contentID'))+'&lang='+$('#UI_LANG').val(),
			'modal' 	 : '#ajaxContentPopup',
			'align'		 : 'center',
			'close'		 : '.modalclose',
			'mask'		 : '#headermask',
			'callClose'  : function(){
				$('#ajaxContentPopup').remove();
			}
		});
	});
	
	
});
