var Sawa = {
	currSlide: 1,
	init: function() {
		if ($('#twitter').length) {
      $("#twitter").tweet({
        join_text: "auto",
        username: "sawatradebody",
        count: 3,
				template: "{time}: {text}",
        loading_text: "Loading tweets...<br />"
      });
		}

		if ($('.dataset_left #cmenu').length) {
			$('.dataset_left #cmenu>LI>A').each(function() {
				$(this).bind('click', function() {
					var self = this;
					$('.dataset_left .dataset LI A').each(function() {
						if (self != this) $(this).parent().removeClass('active');
					});
					$(this).parent().toggleClass('active');
					return false;
				});
			});
		}

		if ($('.rightmenu').length) {
			if ($('BODY.page-id-133, BODY.page-id-540').length) {
			} else {
				$('.content').css('width', 705);
			}
		}

		if ($('.main H1').height() > 56) {
			$('.main H1').css('display', 'block');
		}


		$("UL.menu>LI>A").each(function() {
			var href = $(this).attr('href');
			if (href == '#') $(this).attr('class', 'dropdown');
			else if (href.indexOf('/contact')>-1) $(this).css('color', 'white');
			else if (href.indexOf('/showcase')>-1) $(this).css('color', 'white');
		});
    
    $("UL.menu>LI>UL>LI>A").each(function() {
      var href = $(this).attr('href');
      if (href == '#') $(this).attr('class', 'header');
      else if (href == '#dummy') $(this).remove();
//      else if (href.indexOf('/contact')>-1) $(this).css('color', 'white');
    });
    
    if ($("#select_country").length) {
      $("#select_country").ufd({
        skin: "country",
        manualWidth: 250
      });

      $("#select_country OPTION").each(function() {
        var $i = $(this).index();
				if ($i > 0) {
	        $("#ufd-container .country li[name="+$i+"]").attr("ID", $(this).attr('value'));
  	      $("#ufd-container .country li[name="+$i+"]").prepend("<div></div>");
				}
      });

			$('#select_country').bind('change', function() {
				$value = $(this).attr('value');
				if ($value) {
					$('DIV.inner DIV').each(function() {
						if ($(this).hasClass('member')) {
							if ($(this).hasClass($value)) $(this).show();
							else $(this).hide();
						}
					});
				} else {
					$('DIV.inner DIV').each(function() {
						if ($(this).hasClass('member')) $(this).show();
					});
				}
			});
    }  
    
		if ($("#select_membertype").length) {
      $("#select_membertype").ufd({
        manualWidth: 300
      });
 
			$('#select_membertype').bind('change', function() {
				if ($(this).attr('value') == 1) {
					document.location.href = '/member/global-screen-advertising-companies';					
				} else {
					document.location.href = '/member/associated-companies';
				}
			});
    }

		if ($("#trailers").length) {
//			$("#trailers").imBookFlip({
////				page_class: 'trailer'
	//		});
		}
    
    Cufon.replace('DIV.menu-header>UL>LI>A, DIV.studios>H3, DIV.rightmenu>H2, DIV.form H4, DIV.dataset_left H2, DIV.dataset_right H2');
		

		if ($('.members H1').length) {
			var e = $('.members H1');
			e.css('white-space','nowrap');
			var fs = 44;
			do {
				e.css('font-size', fs);
				Cufon.refresh('.members H1');
				fs--;
			} while (e.width() > 745);
		}

		if ($('.big_front').length) this.slideshowTimer = setTimeout("Sawa.slide()", 5000);

		$(window).resize($.proxy(this.doResize, this));
    this.doResize();
	},

	slide: function(nr) {
		var self = this;
		var c = this.currSlide;
		var t = $('.big_front DIV').length;

		if (nr) this.currSlide = nr;
		else this.currSlide++;

		if (this.currSlide > t) this.currSlide=1;		

		$('#title'+c).animate({opacity: 0}, function() {
			$(this).hide();
			$('#title'+self.currSlide).css({opacity: 0, 'margin-left': -500}).show().animate({opacity: 1, 'margin-left': 0},500,'easein');
		});

		$('#image'+c).animate({opacity: 0}, function() { $(this).hide(); });
		$('#tab'+c).removeClass('active');

		$('#image'+this.currSlide).css('opacity',0).show().animate({opacity: 1},1000,'easein');
		$('#tab'+this.currSlide).addClass('active');

		clearTimeout(this.slideshowTimer);
		this.slideshowTimer = setTimeout("Sawa.slide()", 5000);

		return false;
	},

	/* FRONT VIDEO PLAYER */
	playYoutube: function(code) {
		clearTimeout(this.slideshowTimer);
		$('.big_front').hide();
		$('DIV.youtubeplayer IFRAME').attr('src', 'http://www.youtube.com/embed/' + code + '?rel=0&amp;hd=1&amp;autoplay=1');
		$('DIV.youtubeplayer').show();
		return false;
	},

	closeYoutube: function() {
		$('DIV.youtubeplayer').hide();
		$('DIV.youtubeplayer IFRAME').attr('src', '');
		$('.big_front').show();

		if ($('.big_front').length) this.slideshowTimer = setTimeout("Sawa.slide()", 5000);
	},

	playVimeo: function(code) {
		clearTimeout(this.slideshowTimer);
		$('.big_front').hide();
		$('DIV.youtubeplayer IFRAME').attr('src', 'http://player.vimeo.com/video/' + code + '?autoplay=1&amp;portrait=0');
		$('DIV.youtubeplayer').show();
		return false;
	},

	/* FRONT TRAILER BROWSER */
	trailerPage: function(d) {
		if (d) {
			$('DIV.trailer').animate({
				left: -310
			},2000,'backout');
		} else {
		}
	},

	/* JOIN FORM */
	submitJoinForm: function() {
		var options = {};
		var fields = ['company_name', 'business_type', 'contact_name', 'address', 'ccountry', 'telephone', 'email'];
		for (i in fields) {
			options[fields[i]] = $('INPUT[name="'+fields[i]+'"]').attr('value');
		}
		$.getJSON('/api/sawa/join_form/', options, $.proxy(this.returnJoinForm, this));

		return false;
	},

	returnJoinForm: function(data) {
		if (data.status == 'ok') {
			$('DIV#join_form INPUT[type="text"]').css('border-color', '#AAAAAA');
			if (data.success == false) {
				$('INPUT[name="'+data.errfield+'"]').css('border-color', 'red').focus();
			} else {
				$('DIV#join_form DIV.inner').animate({height: 40}, 1200, 'bounceout', function() {
					$(this).html('<p><strong>Thank you</strong></p><p>You will be contacted in the near future</p>');
				});
			}
		} else {
			alert('An unknown error occured. Please try again later.');
		}
	},

	/* CONTACT FORM */
	submitContactForm: function() {
		var options = {};
		var fields = ['nname', 'company', 'ccountry', 'email', 'enquiry'];
		for (i in fields) {
			options[fields[i]] = $('#contact_form [name="'+fields[i]+'"]').attr('value');
		}
		$.getJSON('/api/sawa/contact_form/', options, $.proxy(this.returnContactForm, this));

		return false;
	},

	returnContactForm: function(data) {
		if (data.status == 'ok') {
			$('DIV#contact_form INPUT[type="text"], DIV#contact_form TEXTAREA').css('border-color', '#AAAAAA');
			if (data.success == false) {
				$('#contact_form [name="'+data.errfield+'"]').css('border-color', 'red').focus();
			} else {
				$('DIV#contact_form DIV.inner').animate({height: 40}, 1200, 'bounceout', function() {
					$(this).html('<p><strong>Thank you</strong></p><p>You will be contacted in the near future</p>');
				});
			}
		} else {
			alert('An unknown error occured. Please try again later.');
		}
	},

	doResize: function() {
		var h = Math.max(
      Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
      Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
      Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );

		t = $('.footer').position().top+$('.footer').height();

		if (t < (h-100)) {
			$('.footer').css('position', 'absolute');
		} else {
			$('.footer').css('position', 'static');
		}
	}
}

$(document).ready(function() {
	Sawa.init();
});
