// JavaScript Document

//Global Variables

var galleryOpened = false;

//Global Variables
 function getAnchor(){
	 
	if(window.location.hash.replace("#", "")) {
  		sayfaYukle(window.location.hash.replace("#", "").toLowerCase());
	} else {
		
	} 
 }
 
 function galeriAyarla(){
	 $("a[rel=galeri]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition'		: 'over',
		'overlayColor'		: '#434343',
		'overlayOpacity'	: 0.9,
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
		return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
 }
 
 $(window).hashchange( function(){
	 getAnchor();
	 });
	 
 function sayfaYukle(sayfa){
	 switch(sayfa)
	 {
		case "paintings":
		case "sketches": 
		case "graphics":
		case "interiors":
		case "furnitures":
		case "sculptures":
		case "architectural":
		case "preeducational":
			yukleGaleri('archiveNew.asp?id='+sayfa+'&h='+($('#contentContainer').height()-20)+'&w='+($('#contentContainer').width()-20));
			break;
		case "contactform":
			yukleContact('contactform.html?t=');
			break;
		case "contactinformation":
			yukle('contact.html');
			break;
		case "companyabout":
			yukle('company.html?t=');
			break;
		case "companycontact":
			yukle('companycontact.html?t=');
			break;
		case "furniture":
		case "kidsfurniture":
		case "lighting":
		case "sculpture":
		case "decorativeobjects":
		yukleKatalog('http://yilmazzenger.com/catalog.asp?name='+sayfa);
			break;
		case "main":
			yukle('http://yilmazzenger.com/haber.asp');
			break;
		default:
			yukle('http://yilmazzenger.com/haber.asp');
			break;
	 }
 }
 
 
 
  $(document).ready(function() 
	{
		if(document.location.href.indexOf("www") != -1){document.location.href = "http://yilmazzenger.com";}
		if(window.location.hash == '' || window.location.hash == '#'){window.location.hash = '#main';}
		ayarla();	
			$( "#menu" ).accordion({
				active: false,
				autoHeight: false,
				navigation: true
			});
		
		getAnchor();
	});
	
  function ayarla(){
		$('#main').height($(window).height()-30);
		$('#main').css('margin-top', '20px');
		$('#contentContainer').height($('#main').height()-75);
		$('#contentContainer').width($('#main').width()-$('#menu').width()-100);
		$('#header').css('width', '285px');
		$('#menu').css('width', '315px');
		$('#menu').css('bottom', '30px');
		//$('#contentContainer').css('background-color', '#434343');
		$('#contentContainer').css('padding-top', '10px');
		$('#contentContainer').css('padding-bottom', '10px');
		$('#content').css('margin-top', ((($('#contentContainer').height()-$('#content').height())/2)-20)+'px');
	 } 

  
  function Load(){
	window.location.href = window.location;
  }
  
  function checkCatalog(){
	if(galleryOpened==true){
		$("#content").css("height","");
		galleryOpened=false;
	} 
  }
  
  function yukleContact(url){
	  checkCatalog();
	  $('#content').load(url, function(){Cufon.replace('#content');ayarla();});
  }
  
  function yukleGaleri(url){
	  checkCatalog();
	  $('#content').load(url, function(){ayarla(); galeriAyarla();});
  }
  
  function yukleKatalog(url){
	  $('#content').load(url, function(){$('#content').height($('#main').height()-95);ayarla(); galleryOpened = true;});  
  }
  
  function yukle(url){
	  checkCatalog();
	  $('#content').load(url, function(){Cufon.replace('#content'); ayarla();});  
  }
  
  function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
  
  function checkMail(i, m, me){
	  if(i=="name" || m=="e-mail address" || me=="message"){alert('please enter your information and message then try again'); return false;}
	  if(!isValidEmailAddress(m)){alert('please check your e-mail address. seems it\'s wrong.'); return false;}
  }
  
  function mailGonder(isim, mail, mesajj, liste){
	  if(checkMail(isim,mail,mesaj) == false){return false;}
	  $('#sendBtn').css('display', 'none');
	  $('#adsoyad').css('display', 'none');
	  $('#email').css('display', 'none');
	  $('#mesaj').css('display', 'none');
	  $('#news').css('display', 'none');
	  $('#newsSelect').css('display', 'none');
	  $('#loadingImg').css('display', 'block');
	  
	  $('body').css('cursor', 'progress');
	  $.post("mailgonder.asp", { adsoyad: isim, email: mail, mesaj: mesajj, newsletter: liste},
   function(data) {
     if(data=='1'){alert('your message has been successfully sent!');  window.location.hash ='main'; $('body').css('cursor');}
	 else{alert('error! please try again.\n\ninfo: '+data);  $('#sendBtn').css('display', 'block'); $('#adsoyad').css('display', 'block'); $('#email').css('display', 'block'); $('#mesaj').css('display', 'block'); $('#news').css('display', 'block'); $('#newsSelect').css('display', 'block'); $('#loadingImg').css('display', 'none'); $('body').css('cursor', 'default');}
   });
	return false;
  }
  
function mailGonder(isim, mail, mesajj, liste){
	  if(checkMail(isim,mail,mesajj) == false){return false;}
	  $('#sendBtn').css('display', 'none');
	  $('#adsoyad').css('display', 'none');
	  $('#email').css('display', 'none');
	  $('#mesaj').css('display', 'none');
	  $('#news').css('display', 'none');
	  $('#newsSelect').css('display', 'none');
	  $('#loadingImg').css('display', 'block');
	  
	  $('body').css('cursor', 'progress');
	  $.post("mailgonder.asp", { adsoyad: isim, email: mail, mesaj: mesajj, newsletter: liste},
   function(data) {
     if(data=='1'){alert('your message has been successfully sent!');  window.location.hash ='main'; $('body').css('cursor');}
	 else{alert('error! please try again.\n\ninfo: '+data);  $('#sendBtn').css('display', 'block'); $('#adsoyad').css('display', 'block'); $('#email').css('display', 'block'); $('#mesaj').css('display', 'block'); $('#news').css('display', 'block'); $('#newsSelect').css('display', 'block'); $('#loadingImg').css('display', 'none'); $('body').css('cursor', 'default');}
   });
	return false;
  }
  
  $(window).resize(function() {ayarla();});
  
  
  function activate(){

	  replacejscssfile("js/ui.coverflow.js", "js/ui.coverflow.js", "js");
	  replacejscssfile("js/app.js", "js/app.js", "js");
	  ayarla();

	  
  }
  
  function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

	function createjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createjscssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}



