var id_curr=0;
var id_prev=0;
var id_next=1;
var objImagePreloader = new Image();

Date.firstDayOfWeek = 0;
Date.format = 'mm/dd/yyyy';


function ajaxRequest(urli,datai) {
	var aj = $.ajax({
		  type: "get",
		  url: urli,
		  data: datai,
		  dataType: "json",
		  success: getResponse,
		  beforeSend: showLoad
	});
}



function getResponse(oReq) {
	var json = oReq;
	if (json.status=="1"){
		id_curr=json.curr;
		id_prev=json.prev;
		id_next=json.next;
		

		objImagePreloader.onload = function() {
			$('#big_img').attr('src',objImagePreloader.src);
			objImagePreloader.onload=function(){};
			$('#ico-loading').hide();
			$('#big_img').show();
			//$('a.work_box').lightBox();
		}

		
		objImagePreloader.src = json.photo;
		$('#pr_main_img').attr('href',json.photo_big);
		$('#pr_main_img').attr('title',json.title);
		
				
		if (id_prev==0&&id_curr==0) $('a.pr-prev').addClass('pr-prev-disabled');
		else $('a.pr-prev').removeClass('pr-prev-disabled');
		
		if (id_next==0) $('a.pr-next').addClass('pr-next-disabled'); 
		else $('a.pr-next').removeClass('pr-next-disabled');
		
	}else{

	}
}


function showLoad () {
	$('#big_img').hide();
	$('#ico-loading').show();
	
}


function getPrev(){
	getImage(id_prev);
}

function getNext(){
	getImage(id_next);
}

function getImage(id){
	$('img.curr_img').css('opacity','0.5');
	$("img.curr_img").mouseover(
		function(){
			$(this).animate({ opacity: 1 }, 500 );
		}
	);
	$("img.curr_img").mouseout(
		function(){
			$(this).animate({ opacity: 0.5 }, 500 );
		}
	);
	$('.thumb_img').removeClass('curr_img');
	$('#img_'+id).addClass('curr_img');
	$('img.curr_img').css('opacity','1');
	$('img.curr_img').unbind("mouseover");
	$('img.curr_img').unbind("mouseout");
	


	
	ajaxRequest('/photos/'+type+'/'+id_link+'/'+id+'/', '');
		
	/*if (id!=0) {
		ajaxRequest('/restaurant/'+id_link+'/'+id+'/', '');
	}else{
		id_prev=0;
		id_curr=0;
		id_next=1;
		$('a.pr-prev').addClass('pr-prev-disabled');
		$('a.pr-next').removeClass('pr-next-disabled');
	}*/
	
}

	
	function load() {
		doLoad();
		mArray.push("39.18829;-106.822901;<span style='color:#000'><b>Hotel Durant</b><br/>122 East Durant<br/>Aspen, Colorado  81611<br/>p  877.438.7268<br/>f  970.925.8789</span>");
		addMarkers();
	}
	
    function doLoad() {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			//map.setCenter(centerPoint, 16);
			map.addControl(new GScaleControl());
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
		}
    }

	function addMarkers() {
		if (mArray.length) {
			var bounds = new GLatLngBounds();
			for (n=0 ; n < mArray.length ; n++ ) {
				var mData = mArray[n].split(';');
				var point = new GLatLng(mData[0],mData[1]);
				bounds.extend(point);
				var marker = createMarker(point, mData[2]);
				
				map.addOverlay(marker);
				//map.openExtInfoWindow(point,
                //           '<div style="width:100px;font-size:0.8em;">' + mData[2]+  '</div>');
						   map.openInfoWindow(point,
                           '<div style="width:170px;font-size:0.8em;">' + mData[2]+  '</div>', {maxWidth:150});
			}
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
		}
	}
	
	function createMarker(point, title) {
		var marker = new GMarker(point,{title:title});
		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindow(
			'<div style="width:100px;font-size:0.8em;">' + title+  '</div>',{maxWidth:150});
		});
		return marker;
	}
	
// Customize two date pickers to work as a date range 
function customRange(input) { 
    return {minDate: (input.id == 'DateIn' ? $('#DateIn').getDatepickerDate() : null), 
        maxDate: (input.id == 'DateOut' ? $('#DateOut').getDatepickerDate() : null)}; 
} 


function mktime () {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: baris ozdil
    // +      input by: gabriel paderni
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FGFEmperor
    // +      input by: Yannoo
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: jakes
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Marc Palau
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: 3D-GRAF
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: mktime(14, 10, 2, 2, 1, 2008);
    // *     returns 1: 1201871402
    // *     example 2: mktime(0, 0, 0, 0, 1, 2008);
    // *     returns 2: 1196463600
    // *     example 3: make = mktime();
    // *     example 3: td = new Date();
    // *     example 3: real = Math.floor(td.getTime()/1000);
    // *     example 3: diff = (real - make);
    // *     results 3: diff < 5
    // *     example 4: mktime(0, 0, 0, 13, 1, 1997)
    // *     returns 4: 883609200
    // *     example 5: mktime(0, 0, 0, 1, 1, 1998)
    // *     returns 5: 883609200
    // *     example 6: mktime(0, 0, 0, 1, 1, 98)
    // *     returns 6: 883609200

    var no=0, i = 0, ma=0, mb=0, d = new Date(), dn = new Date(), argv = arguments, argc = argv.length;

    var dateManip = {
        0: function (tt){ return d.setHours(tt); },
        1: function (tt){ return d.setMinutes(tt); },
        2: function (tt){ var set = d.setSeconds(tt); mb = d.getDate() - dn.getDate(); d.setDate(1); return set;},
        3: function (tt){ var set = d.setMonth(parseInt(tt, 10)-1); ma = d.getFullYear() - dn.getFullYear(); return set;},
        4: function (tt){ return d.setDate(tt+mb);},
        5: function (tt){
            if (tt >= 0 && tt <= 69) {
                tt += 2000;
            }
            else if (tt >= 70 && tt <= 100) {
                tt += 1900;
            }
            return d.setFullYear(tt+ma);
        }
        // 7th argument (for DST) is deprecated
    };

    for (i = 0; i < argc; i++){
        no = parseInt(argv[i]*1, 10);
        if (isNaN(no)) {
            return false;
        } else {
            // arg is number, let's manipulate date object
            if (!dateManip[i](no)){
                // failed
                return false;
            }
        }
    }
    for (i = argc; i < 6; i++) {
        switch (i) {
            case 0:
                no = dn.getHours();
                break;
            case 1:
                no = dn.getMinutes();
                break;
            case 2:
                no = dn.getSeconds();
                break;
            case 3:
                no = dn.getMonth()+1;
                break;
            case 4:
                no = dn.getDate();
                break;
            case 5:
                no = dn.getFullYear();
                break;
        }
        dateManip[i](no);
    }

    return Math.floor(d.getTime()/1000);
}
 function explode (delimiter, string, limit) {
    // http://kevin.vanzonneveld.net
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||
        typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}


function SendFormData(){
	var form = $('#iSTAYform');
	var DateIn = $('#DateIn').val();
	var DateOut = $('#DateOut').val();
	
	in_arr = explode ('/',DateIn);
	out_arr = explode ('/',DateOut);
	
	in_time = mktime(0, 0, 0, in_arr[0], in_arr[1], in_arr[2]);
	out_time = mktime(0, 0, 0, out_arr[0], out_arr[1], out_arr[2]);
	
	days = Math.ceil((out_time-in_time)/86400);
	
	$('#Length').val(days);
	
	form.submit();
	return 0;

}
	
jQuery (document).ready(function(){
		$('.media').media({height:420,width:734});
		$.ifixpng('/images/pixel.gif');
		$('img[src$=.png]').ifixpng();
		$('a.work_box').lightBox();
		$('#scroll_pane').jScrollPane({scrollbarWidth:12});

		
		$(".sf-menu")
			.superfish({
				animation : { opacity:"show",height:"show"},
				autoArrows : false,
				dropShadows : false
			}).css('opacity',0.9);
	
	$('img.thumb_img').css('opacity','0.5');
	$("img.thumb_img").mouseover(
		function(){
			$(this).animate({ opacity: 1 }, 500 );
		}
	);
	$("img.thumb_img").mouseout(
		function(){
			$(this).animate({ opacity: 0.5 }, 500 );
		}
	);
	$('img.curr_img').css('opacity','1');
	$('img.curr_img').unbind("mouseover");
	$('img.curr_img').unbind("mouseout");
	
	
	$('.date').datePicker();
	$('#DateIn').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#DateOut').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#DateOut').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#DateIn').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);

	
	//$.datepicker.setDefaults({showOn: 'both', buttonImageOnly: true,  buttonImage: '/images/calendar.gif', buttonText: 'Calendar'});
	//$('.date').attachDatepicker({beforeShow: customRange}); 
	
 
		
});
	