/*
 * jQuery Image Gallery Plugin JS Example 2.0
 * https://github.com/blueimp/jQuery-Image-Gallery
 *
 * Copyright 2011, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://creativecommons.org/licenses/MIT/
 */

/*jslint white: true, unparam: true, regexp: true */
/*global $, document, window */

$(function () {
    'use strict';
	
	setTimeout(function(){	$("#logo").css('opacity', '1.0'); $("#logo").effect('slide', {easing: 'easeOutQuint'}, 1000, null); }, 2000);
	
	//hover for social icons div
	$("#socialIcons").hover(function(){ $(this).animate({opacity: "1.0"}); }, function(){ $(this).animate({opacity: "0.7"}); });
	//hide the image error now, show it when you need it
	$("#imageLoadError").hide();
	// Error Dialog	
	$('#dialog').dialog({
		autoOpen: false,
		width: 510,
		closeOnEscape: true,
		modal: true,
		resizable: false,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	$('#photoGallery').imagegallery();
    // Initialize the Image Gallery widget with default values
    $('#photoGallery').imagegallery('option', {
            show: showEffect,
            hide: hideEffect,
            fullscreen: fullScreen,
            slideshow: defaultDelay * 1000,
            closeCallback: slideshowClosed
    });
	
    $('#slider').slider({
					range: false,
					value: defaultDelay,
					animate: true,
					min: 2.5,
					max: 5,
					step: 0.1
	});
				
	//hover states on the static widgets
	$('div#slideshowControls div#controlsDiv').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
	$("#slideshowDelay").text(overrideDelay + "s");
	
	//radio button choices
	$("#radioset").buttonset();
	
	$( "#slider" ).bind( "slidechange", function(event, ui) {
		imageGallery(overrideDelay);
	});
	
	$( "#slider" ).bind( "slide", function(event, ui) {
		overrideDelay = ui.value;
		$("#slideshowDelay").text(overrideDelay + "s");
	});
	
	$("#play").click(function(){
		if($(this).attr("class") == "ui-icon ui-icon-play")
			$(this).attr("class", "ui-icon ui-icon-pause");
		else{
			$(this).attr("class", "ui-icon ui-icon-play");
			return false;
		}
		$('a[rel="gallery"]').trigger("click");
	}); 
	
	//extruders
	$("#aboutUs").hover(function(){$(this).animate({opacity: "1.0"})}, function(){$(this).animate({opacity: "0.7"})});
	$("#aboutUs").buildMbExtruder({
        position:"left",
        width:350,
        extruderOpacity:.7,
        hidePanelsOnClose:true,
        accordionPanels:true,
        autoCloseTime:0,
        onExtOpen:function(){},
        onExtContentLoad:function(){},
        onExtClose:function(){}
      });
      
	  $("#contactUs").hover(function(){$(this).animate({opacity: "1.0"})}, function(){$(this).animate({opacity: "0.7"})});
	  $("#contactUs").buildMbExtruder({
	    position:"left",
	    width:350,
	    extruderOpacity:.7,
	    hidePanelsOnClose:true,
	    accordionPanels:true,
	    autoCloseTime:0,
	    onExtOpen:function(){},
	    onExtContentLoad:function(){},
	    onExtClose:function(){}
	  });
	 
	 $("#rates").hover(function(){$(this).animate({opacity: "1.0"})}, function(){$(this).animate({opacity: "0.7"})});
	 $("#rates").buildMbExtruder({
	    position:"left",
	    width:350,
	    extruderOpacity:.7,
	    hidePanelsOnClose:true,
	    accordionPanels:true,
	    autoCloseTime:0,
	    onExtOpen:function(){},
	    onExtContentLoad:function(){},
	    onExtClose:function(){}
	  });
	 
	 $("#learnMore").hover(function(){$(this).animate({opacity: "1.0"})}, function(){$(this).animate({opacity: "0.7"})});
	 $("#learnMore").buildMbExtruder({
	    position:"left",
	    width:350,
	    extruderOpacity:.7,
	    hidePanelsOnClose:true,
	    accordionPanels:true,
	    autoCloseTime:0,
	    onExtOpen:function(){},
	    onExtContentLoad:function(){},
	    onExtClose:function(){}
	  });

	//end extruders
});

function slideshowClosed(){
	$("#play").attr("class", "ui-icon ui-icon-play");
}

function loadImages(){
	// Load images via flickr for demonstration purposes:
    $.ajax({
        url: 'http://api.flickr.com/services/rest/',
        data: {
            format: 'json',
            method: 'flickr.people.getPublicPhotos',
            api_key: 'bec89376d56a5d836073c22393615349',
            user_id: '71809771@N05',
            per_page: '100'
        },
	    dataType: 'jsonp',
        jsonp: 'jsoncallback',
	    success: function (data) {
            $.each(data.photos.photo, function (index, photo) {
                var url = 'http://farm' + photo.farm + '.static.flickr.com/' +
                    photo.server + '/' + photo.id + '_' + photo.secret;
                $('<a rel="gallery"></a>')
                    .append($('<img onmouseover="popOut(this)" onmouseout="popIn(this)">').prop('src', url + '_s.jpg').prop('id', index + '_img'))
                    .prop('href', url + '_b.jpg')
                    .prop('title', photo.title)
                    .prop('id', index + '_a')
                    .appendTo('#photoGallery');
            });
        },
        error: function(data){
        	$("#imageLoadError").show();
        	$('#dialog').dialog('open');
        }
    });
}

function loadVideos(hide){
	if(typeof hide == 'boolean' && !hide){
		$("iframe").show();
	}
	else if(typeof hide == 'boolean' && hide){
		$("iframe").hide();
	}
}

function showDiv(divId, callback){
	currentDiv = divId;
    $(divId).show();
	$(divId).effect('slide', {easing: 'easeOutQuint'}, 1000, null);
	$(divId).css("z-index", "99");
	$("#main").css("z-index", "0");
	if(callback)
		setTimeout(callback, 500);
	
	return true;
}

function hideDiv(callback){
	var mainDivId = "#main";
	//hide the body's scrollbar so when effect is playing it doesn't show
	//it will be restored in the effect's 'complete' callback
	$('#docbody').css('overflow', 'hidden');
	setTimeout(function(){
		$(currentDiv).effect('explode', {easing: 'easeOutQuint'}, 500, function(){$('#docbody').css('overflow', 'auto');});
		$(currentDiv).css("z-index", "0");
		$(currentDiv).hide();
		$(mainDivId).css("z-index", "99");
		currentDiv = "";
		if(callback)
			setTimeout(callback, 500);
	}, 50);
	
	return true;
}

function imageGallery(delay){
	$('#photoGallery').imagegallery('option', {
            slideshow: delay * 1000
    });
}

function popOut(obj){
	$(obj).animate({
    width: 115,
    height: 115
  	}, 200, 'linear', function() {    
  });
}

function popIn(obj){
	$(obj).animate({
    width: 75,
    height: 75
  	}, 200, 'linear', function() {    
  });

}

function iconsMouseOver(id){
	$(id).animate({opacity: '1.0'});	
}

function iconsMouseOut(id){
	$(id).animate({opacity: '0.7'});
}

function openContactForm(){
	$("#rates").closeMbExtruder();
	setTimeout(function(){ $("#contactUs").openMbExtruder(true); }, 500);
}

function changeVideoSrc(obj){
	var source = obj.attributes.iframe_src.value;
	if(source && source != ""){
		$('#videoFrame').attr('src', source);
		$('#videoFrameDiv').effect('slide', {easing: 'easeOutQuint'}, 1000, null);
	}
}
