var set_url = "";
var first_flag=true;
$(function(){
	var current = 1;
	var count = 0;
	var overlay = $('#overlay').css({opacity:0.7, height:$(document).height()});
	
	$('#slide-images li a, #content h2>a, #btn-see-images>a, #btn-manifesto>a').click(function(){
	  var id = $($(this).attr('href'));
	  count = id.attr('id')=='fw2010campaign'?  19:24;
	  overlay.fadeIn(600,function(){
      id.fadeIn(600,function(){
        $('.closebtn>a',id).click(function(){
          id.fadeOut(600,function(){ 
            overlay.fadeOut(600,function(){
              current = 2;
              $('.back',id).show(600,imgBack(id));  
            })
          });
          return false;
        });
        $('.back',id).unbind('click').click(imgBack(id)); 
        $('.next',id).unbind('click').click(imgNext(id));
      })
	  })
	  return false;
	});
		
	function imgBack(id){
	  return function(){
	    var name = id[0].id, str = "", txt = "";
      var ipath = "../img/be_stupid/"+ name +"/";
  	  if(current-1>=1){
        current -= 1;
        var c = strlen(current);
        str = [ipath,c,".jpg"].join('');
        $('.imagewrap img',id).attr('src',str);
        if(ipath.indexOf('fw2010campaign') != -1 ){
          txt = [ipath,"txt",c,".gif"].join('');
          $('#textbox img',id).attr('src',txt);
        } 
        navChange('back',this,id);
      }
    return false;  
	  }
	  
	}
	function imgNext(id){
	  return function(){
	    var name = id[0].id, str = "", txt = "";
	    var ipath = "../img/be_stupid/"+ name +"/";
	    if(current+1<=count){
        current += 1;
        var c = strlen(current);
        str = [ipath,c,".jpg"].join('');
        $('.imagewrap img',id).attr('src',str);
        if(ipath.indexOf('fw2010campaign') != -1){
          txt = [ipath,"txt",c,".gif"].join('');
          $('#textbox img',id).attr('src',txt);
        } 
        navChange('next',this,id);   
	   }
	   return false;
	 }
	}
	
  function navChange(nav,obj,id){
    if(current==count || current == 1){
      if(!$("img",obj).attr('src').match('_none')) $("img",obj).attr('src',$("img",obj).attr('src').replace('.gif','_none.gif'));
    } else {
      if($('.next img',id).attr('src').match('_none')) $('.next img',id).attr('src',$('.next img',id).attr('src').replace('_none.gif','.gif'));
      if($('.back img',id).attr('src').match('_none')) $('.back img',id).attr('src',$('.back img',id).attr('src').replace('_none.gif','.gif'));
    }  
  }
	function strlen(str){
	  str = String(str);
	  if(str.length == 1) str = "0" + str;
	  return str;
	}
		
	//movie action
	var movie = $('#mov-countryside');
	$('#movbanner li').click(function(){
	   var id = this.id;
	   var url = "";   
	   overlay.fadeIn(600,function(){
	   // create video tag q
	     if($.browser.webkit){
	       id += ".mp4";
	       set_url = id;
	       url = location.protocol + "//" + location.host + "/be-stupid/"
	       $('video',movie)[0].src = [url,'mov',id].join('/');
	     } else {
	       id += ".flv";
	       set_url = id;
	     } 
       $('.movie',movie).show(); 
	     movie.fadeIn(300,function(){       
          $('html, body').animate({scrollTop: $('#mov-countryside').offset().top - 50},600);
          if($.browser.webkit && typeof $('video',movie)[0].play == 'function'){
            $('video',movie)[0].play();
          } else {
            first_flag || $.browser.mozilla || $.browser.safari ? first_flag = false: set_complete();
          }
	     });
	   });
	})
	
	$('.btn-mov-close',movie).click(function(){
	 if(!first_flag) document["movie"].js_stop();
	 movie.fadeOut(600,function(){
	   if($.browser.webkit && typeof $('video',movie)[0].play == 'function') $('video',movie)[0].pause();
	   overlay.fadeOut(600);
	 })
	})
	
	//social link
	var title = document.title, shortUrl = "http://bit.ly/deVCoc", social = $('.share a');
	var so = {twitter: 'http://twitter.com/home/?status=', facebook: 'http://www.facebook.com/sharer.php?u=',mixi: 'http://www.diesel.co.jp/be-stupid/pcgen/mixi/?'};
	social.click(function(){
	  var api = so[this.rel];
    switch(this.rel) {
    case 'twitter':
      api += encodeURIComponent(title + ' ' + shortUrl);
      window.open(api)
      break;
    case 'facebook':
      api += encodeURIComponent(shortUrl);
      window.open(api)
      break;
    case 'mixi':
      $('#mixi').css({'z-index':10000});
      $('#mixi #text').val(shortUrl);
      $('#mixi #title').val(title);
      $('a[rel=mixi]').nyroModalManual({minWidth: 300,minHeight: 360, bgColor: "#000",zIndexStart:1500})
      break;
    }
    return false;
	})

  $('#msed').click(function(){
    var f = $("#form-fields"), error = new Array();
    var api = "http://diesel-api.appspot.com/mixi/json/?callback=?"
    var query = { user: $('#email',f).val(),password: $('#password',f).val(),title: $('#title',f).val(),body: $('#text',f).val()};
    $.each(query,function(i,v){
      if(v=="") error.push(i)
    })
    if(!error.length){
      $.getJSON(api,query,function(res){
        var str = ""
        if(res){
          switch(res.status){
            case "ok":
              str = "書き込みが完了しました。";
            break; 
            case "ng":
              str = "書き込みに失敗しました。";
            break;
            case "loginerr":
              str = "ログインエラー";
            break;
          }
        } else {
          str = '通信エラーが発生しました。';
        }
        $('#text',f).css({opacity: '0.2'})
        $('.message',f).hide().text(str).fadeIn(1500).fadeOut(600,function(){
          $('#text',f).css({opacity: '1'})
        });
      })
    }
    return false;
  })
})

function set_complete(){
  document["movie"].js_start(set_url);
}
