var current_zoom = 0;
var count_zoom = 4;
var site_url = 'http://auksjon2.oslobilauksjon.no/';


// ====================== caroseel ===================

/* Create ImageFlow instances when the DOM structure has been loaded */

domReady(function()
{
//var basic_2 = new ImageFlow();
//basic_2.init({ ImageFlowID: 'myImageFlow',
//                   reflections: false,
//                         slideshow: true,
//                         slideshowAutoplay: true,
//                   reflectionP: 0.0 });

/*
var circular_3 = new ImageFlow();
circular_3.init({ ImageFlowID: 'myImageFlow',
                      circular: true,
					  buttons: true,
                      reflections: false,
                      reflectionP: 0.4,
                      slider: true,
                      captions: false,
                      opacity: true,
                      xStep: 250,
                      imageFocusM: 1.5,
                         slideshow: true,
                         slideshowAutoplay: true,
                      startID: 3
					  });

*/


var circular_3 = new ImageFlow();
circular_3.init({ ImageFlowID: 'myImageFlow',

		animationSpeed:     50,             /* Animation speed in ms */
		aspectRatio:        2.379,          /* Aspect ratio of the ImageFlow container (width divided by height) */
		buttons:            true,          /* Toggle navigation buttons */
		captions:           false,           /* Toggle captions */
		circular:           true,          /* Toggle circular rotation */
		imageCursor:        'pointer',      /* Cursor type for all images - default is 'default' */
		imageFocusM:        1.35,            /* Multiplicator for the focussed image size in percent */
		imageFocusMax:      4,              /* Max number of images on each side of the focussed one */
		imagePath:          '',             /* Path to the images relative to the reflect_.php script */
		imageScaling:       true,           /* Toggle image scaling */
		imagesHeight:       0.72,           /* Height of the images div container in percent */
		imagesM:            1.0,            /* Multiplicator for all images in percent */
		onClick:            function() { openFirstCar() },   /* Onclick behaviour */
		opacity:            true,          /* Toggle image opacity */
		opacityArray:       [10,8,6,4,2],   /* Image opacity (range: 0 to 10) first value is for the focussed image */
		percentLandscape:   118,            /* Scale landscape format */
		percentOther:       100,            /* Scale portrait and square format */
		preloadImages:      true,           /* Toggles loading bar (false: requires img attributes height and width) */
		reflections:        false,           /* Toggle reflections */
		reflectionGET:      '',             /* Pass variables via the GET method to the reflect_.php script */
		reflectionP:        0.4,            /* Height of the reflection in percent of the source image */
		reflectionPNG:      false,          /* Toggle reflect2.php or reflect3.php */
		reflectPath:        '',             /* Path to the reflect_.php script */
		scrollbarP:         0.8,            /* Width of the scrollbar in percent */
		slider:             true,           /* Toggle slider */
		sliderCursor:       'e-resize',     /* Slider cursor type - default is 'default' */
		sliderWidth:        14,             /* Width of the slider in px */
		slideshow:          true,          /* Toggle slideshow */
		slideshowSpeed:     1500,           /* Time between slides in ms */
		slideshowAutoplay:  true,          /* Toggle automatic slideshow play on startup */
		startID:            1,              /* Image ID to begin with */
		glideToStartID:     true,           /* Toggle glide animation to start ID */
		startAnimation:     false,          /* Animate images moving in from the right on startup */
		xStep:             250             /* Step width on the x-axis in px */


					  });

});

var first_car_url = '';

function openFirstCar()
{
	document.location = first_car_url;
}


// ====================== zoomer ===================

$(document).ready(function(){
 	$('ul.thumb li').Zoomer({speedView:200,speedRemove:400,altAnim:false,speedTitle:400,debug:false});

	$('#zoomg').mouseover(function() {
		clearInterval(intervalID_sold_car);
	});
	$('#zoomg').mouseout(function() {
		intervalID_sold_car = window.setInterval("next_zoom()", 4000);
	});

	intervalID_sold_car = window.setInterval("next_zoom()", 4000);
});

// ====================== slider zoomer ===================

function next_zoom()
{
 	$('#content'+current_zoom).fadeOut("slow");
	current_zoom++;

	if (current_zoom > count_zoom - 1)
	{
		current_zoom = 0;
	}

 	$('#content'+current_zoom).fadeIn("slow");
}

function prev_zoom()
{
 	$('#content'+current_zoom).fadeOut("slow");
	current_zoom--;

	if (current_zoom < 0)
	{
		current_zoom = count_zoom - 1;
	}

 	$('#content'+current_zoom).fadeIn("slow");
}

// ======================  video modal ===================

$(document).ready(function(){
		$("a[rel^='video']").prettyPhoto({
			social_tools: '<iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2FOsloBilauksjon&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>'
		});

		$("a[rel^='gallery']").prettyPhoto({
			social_tools: '<iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2FOsloBilauksjon&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>',

			markup: '<div class="gallery-zoom"> \
						<div class="pp_pic_holder"> \
							<div class="ppt">&nbsp;</div> \
							<div class="pp_top"> \
								<div class="pp_left"></div> \
								<div class="pp_middle"></div> \
								<div class="pp_right"></div> \
							</div> \
							<div class="pp_content_container"> \
								<div class="pp_left"> \
								<div class="pp_right"> \
									<div class="pp_content"> \
										<div class="pp_loaderIcon"></div> \
										<div class="pp_fade"> \
											<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
											<div class="pp_hoverContainer"> \
												<a class="pp_next" href="#">next</a> \
												<a class="pp_previous" href="#">previous</a> \
											</div> \
											<div id="pp_full_res"></div> \
											<div class="pp_details"> \
												<div class="pp_nav"> \
													<a href="#" class="pp_arrow_previous">Previous</a> \
													<p class="currentTextHolder">0/0</p> \
													<a href="#" class="pp_arrow_next">Next</a> \
												</div> \
												<div class="div_pp_description"><p class="pp_description"></p></div> \
												<div class="pp_social">{pp_social}</div> \
												<a class="pp_close" href="#">Close</a> \
											</div> \
										</div> \
									</div> \
								</div> \
								</div> \
							</div> \
							<div class="pp_bottom"> \
								<div class="pp_left"></div> \
								<div class="pp_middle"></div> \
								<div class="pp_right"></div> \
							</div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>',


		});

	});


// ======================  subscribe form ===================


$( function() {
	$('#formsubscribe').submit(function() {
		subscribe();
		return false;
	});
});


function subscribe()
{
	$("#newsletter-fb-like").hide();
	$("#newsletter_comment").hide();
	$("#newsletter_error").hide();
	$("#newsletter_form").hide();
	$("#newsletter_loading").show();

	$.post("subscribe.php", {email : $("#subscriber_email").val()},
	function (data)
	{
		if (data.error == 1)
		{
			$("#newsletter_loading").hide();
			$("#newsletter_error").show();
			$("#newsletter_error").html(data.error_comment);
			$("#newsletter_form").show();
		}
		else
		{
			$("#newsletter_loading").hide();
			$("#newsletter_comment").show();
		}

	}, 'JSON');
}


// ======================  search form ===================

var searchcars_properties = new Object;
$(function() {
	$("#searchcars").autocomplete({
		source: function( request, response) {
			$.post("sitesearch.php", {
					search: request.term},
					function( data ) {
					response( $.map(data, function(item) {
						return {
							label: item.name,
							value: item.car_id
						}
					}))},
					"json");
		},
		minLength: 2,
		select: function( event, ui ) {
		    var action = $("#searchcarsform").attr('action');
		    $("#searchcarsform").attr('action', site_url + "cars_info.php?car_id="+ui.item.value);
		    $("#searchcarsform").submit();
		    $("#searchcarsform").attr('action', action);
		    ui.item.value = ui.item.label;
		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});

    $("#searchcars").focus(
    function()
    {
        searchcars_properties.value = $(this).val();
        searchcars_properties.style = $(this).css("color");
        if (searchcars_properties.style == "rgb(170, 170, 170)" || searchcars_properties.style == "#aaaaaa")
        {
            $(this).val("");
            searchcars_properties.value = "";
            $(this).css("color", "#000000");
        }
    });

    $("#searchcars").blur(
    function()
    {
        if ($(this).val() == "" && (searchcars_properties.style == "rgb(170, 170, 170)" || searchcars_properties.style == "#aaaaaa"))
        {
            $(this).css("color", "#aaaaaa");
            $(this).val("Etter biltype, merke, modell og årsmodell");
        }
    });
});


$(document).ready(function(){
		$("#obslider").show();
	});
