jQuery(function() {

	jQuery('#container').height(jQuery(window).height());

/* Empty fields */
	jQuery('input[type="text"], textarea').each(function() { jQuery(this).attr({"defaultvalue":jQuery(this).attr('value')}); });
	jQuery('input[type="text"], textarea').focus(function() { if(jQuery(this).attr("value") == jQuery(this).attr("defaultvalue")) { jQuery(this).attr({"value":""}); jQuery(this).addClass('field_active'); } });
	jQuery('input[type="text"], textarea').blur(function() { if(jQuery(this).attr("value") == "") { jQuery(this).attr({"value":jQuery(this).attr("defaultvalue")}); jQuery(this).removeClass('field_active'); } });


/* Home - Back image change */
	var currenthomethumbid;
	jQuery('.home_thumbs .thumb a.thumb_image').hoverIntent(function() {
		currenthomethumbid = jQuery('.home_thumbs .thumb').index(jQuery(this).parent('.thumb'));
		if(jQuery(this).parent('.thumb').is('.home_thumb_active')) { } else {
			api.goTo(currenthomethumbid+1);
			api.playToggle();
		}
	}, function() { });


/* Slideshow */
	var aboutslideshowduration = jQuery('ul.slideshow').attr('id');
	var aboutslideshowcurrent = 0;
	jQuery('ul.slideshow li').eq(0).show();
	if($('ul.slideshow li').length > 1) {
		setInterval(function() {
			aboutslideshowcurrent++;
			if(aboutslideshowcurrent == jQuery('ul.slideshow li').size()) { aboutslideshowcurrent = 0; }
			jQuery('ul.slideshow li').not(aboutslideshowcurrent).fadeOut(1000);
			jQuery('ul.slideshow li').eq(aboutslideshowcurrent).fadeIn(1000);
		}, aboutslideshowduration);
	}


/* Filter */
	jQuery(".dropdown dt a").click(function() {
		jQuery(this).parent('dt').parent('dl').find('dd').find('ul').slideToggle();
		return false;
	});
    var selecttext = "";
    var selectsource = "";
    jQuery(".field .dropdown dd ul li a").click(function() {
        selecttext = jQuery(this).text();
        jQuery(this).parent('li').parent('ul').parent('dd').parent('.dropdown').find('dt').find('a').html(selecttext);
        jQuery(".dropdown dd ul").hide();
        jQuery(this).parent('li').parent('ul').parent('dd').parent('.dropdown').parent('.field').find('select').val(selecttext);
        jQuery(this).parent('li').parent('ul').find('li').find('a').removeClass('active');
        jQuery(this).addClass('active');
    });
/*
    var selectedoption;
    jQuery('.field').each(function() {
    jQuery(this).find('select option').each(function() {
    	if(jQuery(this).attr('selected') == 'selected') {
    		selectedoption = jQuery(this).text();
    		jQuery(this).parent('select').parent('.field').find('.dropdown').find('dd').find('ul').find('li').find('a').each(function() {
    			if(jQuery(this).text() == selectedoption) { jQuery(this).addClass('active');  jQuery(this).parent('li').parent('ul').parent('dd').parent('.dropdown').find('dt').find('a').html(selectedoption); }
    		});
    	}
	});
    });
*/


/* Work Item */
	var workvideoholderhtml;
	jQuery('.work_content .play_video').click(function() {
		jQuery('.work_slideshow').fadeOut(1000);
		jQuery('.work_vimeo').fadeIn(1000);
		workvideoholderhtml = '<iframe src="http://player.vimeo.com/video/'+jQuery('.work_vimeo').attr('id')+'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1&amp;byline=0&amp;title=0&amp;portrait=0" width="574" height="323" frameborder="0" allowTransparency="true"></iframe>';
		jQuery('.work_vimeo').html(workvideoholderhtml);
	});
	jQuery('a.toggle_credits').click(function() {
		jQuery('.work_credits').slideToggle();
		return false;
	});


/* Thumbs */
	jQuery('.thumb').hover(function() {
		if(jQuery(this).is('.thumb_inplay')) { } else { jQuery(this).addClass('thumb_active'); }
	}, function() {
		if(jQuery(this).is('.thumb_inplay')) { } else { jQuery(this).removeClass('thumb_active'); }
	});

/* rewittern add and remove clips */
	$('.thumb .thumb_clip').click(function(event) {
		event.preventDefault();
		// get length of clips array from #clippeddata
		var cliplist = $('#clippeddata').html();
		var clipsArray = cliplist.split('-');
		if(clipsArray[0].length < 1 ) clipsArray.shift(); // remove phantom empty element
		var clipcount = clipsArray.length;
		var id = $(this).parent().attr('id');
		if($(this).parent().hasClass('thumb_clipped')){
			// remove it
			clipsArray.splice($.inArray(id,clipsArray), 1).join('-');
			$('#clippeddata').html(clipsArray.join('-'));
			$(this).parent().removeClass('thumb_clipped')
			$.post("/clips/update/" + $('#clippeddata').html(), function(response){
				$('#clippeddata').html($.parseJSON(response).clips); // set with the string returned from the server for safety
				$('#clipcounter').html($.parseJSON(response).count);
				if($.parseJSON(response).count == 0) $('#clipcount').hide();
			});
			if($(this).parent().parent().hasClass('clips_thumbs')) {
				$(this).parent().remove();
				$('.clips_thumbs .thumb').removeClass('thumb_last');
				$('.clips_thumbs .thumb:nth-child(3n)').addClass('thumb_last');
				if($('.clips_thumbs .thumb').size() == 0) { $('.clips_video, div.clips').hide(); $('.noclips').show(); $('#bottom').addClass('stick'); }
			}
		}else{
			// add it
			clipsArray.push(id);
			$('#clippeddata').html(clipsArray.join('-'));
			$(this).parent().addClass('thumb_clipped')
			$.post("/clips/update/" + $('#clippeddata').html(), function(response){
				$('#clippeddata').html($.parseJSON(response).clips); // set with the string returned from the server for safety
				$('#clipcounter').html($.parseJSON(response).count);
				if($.parseJSON(response).count > 0) $('#clipcount').show();
				$('#clipplus').html("+");
			});
		}
	});

	$('a.addtoclips').click(function(event){
		event.preventDefault();
		// get length of clips array from #clippeddata
		var cliplist = $('#clippeddata').html();
		var clipsArray = cliplist.split('-');
		if(clipsArray[0].length < 1 ) clipsArray.shift(); // remove phantom empty element
		var clipcount = clipsArray.length;
		var id = $(this).attr('id');
		if($(this).hasClass('removefromclips')){
			// remove it
			clipsArray.splice($.inArray(id,clipsArray), 1).join('-');
			$('#clippeddata').html(clipsArray.join('-'));
			$(this).parent().removeClass('thumb_clipped')
			$.post("/clips/update/" + $('#clippeddata').html(), function(response){
				$('#clippeddata').html($.parseJSON(response).clips); // set with the string returned from the server for safety
				$('#clipcounter').html($.parseJSON(response).count);
				if($.parseJSON(response).count == 0) $('#clipcount').hide();
			});
			$(this).removeClass('removefromclips').html("Add to Clips+");
		}else{
			// add it
			clipsArray.push(id);
			$('#clippeddata').html(clipsArray.join('-'));
			$(this).parent().addClass('thumb_clipped')
			$.post("/clips/update/" + $('#clippeddata').html(), function(response){
				$('#clippeddata').html($.parseJSON(response).clips); // set with the string returned from the server for safety
				$('#clipcounter').html($.parseJSON(response).count);
				if($.parseJSON(response).count > 0) $('#clipcount').show();
				$('#clipplus').html("+");
			});
			$(this).addClass('removefromclips').html("Remove from clips-");
		}
	});


/* Reels & Clips */
	var videoholderhtml;
	jQuery('.video_thumbs .reel_link a, .video_thumbs .thumb_image').click(function() {
		if(jQuery(this).parent('div').is('.thumb_inplay') || jQuery(this).parent('div').parent('div').is('.thumb_inplay')) { return false; } else {
			videoholderhtml = '<iframe src="http://player.vimeo.com/video/'+$(this).attr('data-vimeo')+'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1&amp;byline=0&amp;title=0&amp;portrait=0" width="772" height="434" frameborder="0" allowTransparency="true"></iframe>'
			jQuery('.video_holder iframe').remove();
			jQuery('.video_holder').html(videoholderhtml);
			jQuery('.video_thumbs .thumb').removeClass('thumb_inplay');
			jQuery('.video_thumbs .thumb').removeClass('thumb_active');
			jQuery(this).parent('div').parent('.thumb').addClass('thumb_inplay');
			jQuery(this).parent('.thumb').addClass('thumb_inplay');
			jQuery('html, body').animate({scrollTop: 0 }, 500);
			return false;
		}
	});
	jQuery('.video_thumbs .thumb').eq(0).addClass('thumb_inplay');


/* Valid Email */
	function validEmail(address){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(address) == false) {
			return false;
		} else {
			return true;
		}
	}
	jQuery('.clips_side input[type="submit"]').click(function() {
		var errormessage = "";
		jQuery(this).parent('form').find('input').removeClass('error');

		if(jQuery(this).parent('form').find('input.your_name').val() == "" || jQuery(this).parent('form').find('input.your_name').val() == "Your name") {
			jQuery(this).parent('form').find('input.your_name').addClass('error');
			errormessage = errormessage + '- Please add your name<br/>';
		}
		if(jQuery(this).parent('form').find('input.clips_list_title').val() == "" || jQuery(this).parent('form').find('input.clips_list_title').val() == "Clips list title") {
			jQuery(this).parent('form').find('input.clips_list_title').addClass('error');
			errormessage = errormessage + '- Please add a clips title<br/>';
		}
		if(validEmail(jQuery(this).parent('form').find('input.your_email').val())) { } else {
			jQuery(this).parent('form').find('input.your_email').addClass('error');
			errormessage = errormessage + '- Please use a valid email<br/>';
		}

		if(jQuery(this).parent('form').find('input.share_email').val() == "" || jQuery(this).parent('form').find('input.share_email').val() == "Share email (optional)") { } else {
			if(validEmail(jQuery(this).parent('form').find('input.share_email').val())) { } else {
				jQuery(this).parent('form').find('input.share_email').addClass('error');
				errormessage = errormessage + '- Please use a valid share email<br/>';
			}
		}
		if(errormessage) {
			jQuery('p.errormessage').remove();
			jQuery(this).parent('form').prepend("<p class='errormessage' style='color:#d57070'>"+errormessage+"</p>");
			return false;
		} else {
			return true;
		}
	});
	jQuery('.newsletterform form input[type="submit"]').click(function() {
		if(validEmail(jQuery(this).parent('form').find('input').eq(0).val())) {
			return true;
		} else {
			jQuery(this).parent('form').find('input').eq(0).css({"background":"#d57070", "color":"#FFF"});
			/* jQuery(this).parent('form').parent('div').prepend("<p style='color:#d57070; position:absolute; top:90px; left: 10px; font-size:10px; line-height:13px;'>Please enter a valid email.</p>"); */
			return false;
		}
	});


/* Footer */
	jQuery('a.bottom_widget_toggle_view').click(function() {
		jQuery(this).parent('.bottom_widget').find('.bottom_widget_block').slideToggle();
		return false;
	});


/* Misc */
    jQuery(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (! $clicked.parents().hasClass("dropdown")) { jQuery(".dropdown dd ul").hide(); }
        if (! $clicked.parents().hasClass("bottom_widget")) { jQuery("#bottom .bottom_widget_block").slideUp(); }
    });
	jQuery('#nav ul.nav_menu li.deactivate a').click(function() { return false; });

});


function checkresolution() {
	windowwidth = jQuery(window).width();
	if(windowwidth < 980) {
		jQuery('body').addClass('lowresolution');
	} else {
		jQuery('body').removeClass('lowresolution');
	}
}

jQuery(window).resize(function() {
	checkresolution();
});

jQuery(window).load(function() {

/* Home tagline hide
	if(jQuery('h3.site_tagline').length > 0) {
		setTimeout(function() {
			jQuery('h3.site_tagline').fadeOut();
		}, 8000);
	}
*/

/* Footer */
	var documentheight;
	var windowheight;
	function checkfooter() {
		documentheight = jQuery(document).height();
		windowheight = jQuery(window).height();
		if(documentheight <= windowheight + 4) {
			jQuery('#bottom').addClass('stick');
		} else {
			jQuery('#bottom').removeClass('stick');
		}
		jQuery('#bottom').fadeIn();
	}
	checkfooter();


/* Low Resolution */
	var windowwidth;
	checkresolution();

});

